Skip to content
57 changes: 30 additions & 27 deletions src/main/java/com/google/firebase/auth/FirebaseAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseException;
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.auth.UserRecord.CreateRequest;
import com.google.firebase.auth.UserRecord.UpdateRequest;
import com.google.firebase.auth.internal.FirebaseTokenFactory;
import com.google.firebase.auth.internal.FirebaseTokenVerifier;
import com.google.firebase.internal.FirebaseService;
Expand Down Expand Up @@ -207,17 +209,17 @@ public FirebaseToken then(@NonNull Task<String> task) throws Exception {
* Gets the user data corresponding to the specified user ID.
*
* @param uid A user ID string.
* @return A {@link Task} which will complete successfully with a {@link User} instance.
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance.
* If an error occurs while retrieving user data or if the specified user ID does not exist,
* the task fails with a FirebaseAuthException.
* @throws IllegalArgumentException If the user ID string is null or empty.
*/
public Task<User> getUser(final String uid) {
public Task<UserRecord> getUser(final String uid) {
checkArgument(!Strings.isNullOrEmpty(uid), "uid must not be null or empty");
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
new Continuation<GetTokenResult, User>() {
new Continuation<GetTokenResult, UserRecord>() {
@Override
public User then(Task<GetTokenResult> task) throws Exception {
public UserRecord then(Task<GetTokenResult> task) throws Exception {
return userManager.getUserById(uid, task.getResult().getToken());
}
});
Expand All @@ -227,61 +229,62 @@ public User then(Task<GetTokenResult> task) throws Exception {
* Gets the user data corresponding to the specified user email.
*
* @param email A user email address string.
* @return A {@link Task} which will complete successfully with a {@link User} instance.
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance.
* If an error occurs while retrieving user data or if the email address does not correspond
* to a user, the task fails with a FirebaseAuthException.
* @throws IllegalArgumentException If the email is null or empty.
*/
public Task<User> getUserByEmail(final String email) {
public Task<UserRecord> getUserByEmail(final String email) {
checkArgument(!Strings.isNullOrEmpty(email), "email must not be null or empty");
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
new Continuation<GetTokenResult, User>() {
new Continuation<GetTokenResult, UserRecord>() {
@Override
public User then(Task<GetTokenResult> task) throws Exception {
public UserRecord then(Task<GetTokenResult> task) throws Exception {
return userManager.getUserByEmail(email, task.getResult().getToken());
}
});
}

/**
* Creates a new user account with the attributes contained in the specified {@link User.Builder}.
* Creates a new user account with the attributes contained in the specified
* {@link CreateRequest}.
*
* @param builder A non-null {@link User.Builder} instance.
* @return A {@link Task} which will complete successfully with a {@link User} instance
* @param request A non-null {@link CreateRequest} instance.
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance
* corresponding to the newly created account. If an error occurs while creating the user
* account, the task fails with a FirebaseAuthException.
* @throws NullPointerException if the provided builder is null.
* @throws NullPointerException if the provided user is null.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user => request ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*/
public Task<User> createUser(final User.Builder builder) {
checkNotNull(builder, "builder must not be null");
public Task<UserRecord> createUser(final CreateRequest request) {
checkNotNull(request, "create request must not be null");
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
new Continuation<GetTokenResult, User>() {
new Continuation<GetTokenResult, UserRecord>() {
@Override
public User then(Task<GetTokenResult> task) throws Exception {
String uid = userManager.createUser(builder, task.getResult().getToken());
public UserRecord then(Task<GetTokenResult> task) throws Exception {
String uid = userManager.createUser(request, task.getResult().getToken());
return userManager.getUserById(uid, task.getResult().getToken());
}
});
}

/**
* Updates an existing user account with the attributes contained in the specified
* {@link User.Updater}.
* {@link UpdateRequest}.
*
* @param updater A non-null {@link User.Updater} instance.
* @return A {@link Task} which will complete successfully with a {@link User} instance
* @param request A non-null {@link UpdateRequest} instance.
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance
* corresponding to the updated user account. If an error occurs while updating the user
* account, the task fails with a FirebaseAuthException.
* @throws NullPointerException if the provided updater is null.
* @throws NullPointerException if the provided update is null.
*/
public Task<User> updateUser(final User.Updater updater) {
checkNotNull(updater, "updater must not be null");
public Task<UserRecord> updateUser(final UpdateRequest request) {
checkNotNull(request, "update request must not be null");
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
new Continuation<GetTokenResult, User>() {
new Continuation<GetTokenResult, UserRecord>() {
@Override
public User then(Task<GetTokenResult> task) throws Exception {
userManager.updateUser(updater, task.getResult().getToken());
return userManager.getUserById(updater.getUid(), task.getResult().getToken());
public UserRecord then(Task<GetTokenResult> task) throws Exception {
userManager.updateUser(request, task.getResult().getToken());
return userManager.getUserById(request.getUid(), task.getResult().getToken());
}
});
}
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/com/google/firebase/auth/FirebaseUserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.firebase.auth.UserRecord.CreateRequest;
import com.google.firebase.auth.UserRecord.UpdateRequest;
import com.google.firebase.auth.internal.GetAccountInfoResponse;

import java.io.IOException;
Expand Down Expand Up @@ -68,7 +70,7 @@ class FirebaseUserManager {
this.requestFactory = transport.createRequestFactory();
}

User getUserById(String uid, String token) throws FirebaseAuthException {
UserRecord getUserById(String uid, String token) throws FirebaseAuthException {
final Map<String, Object> payload = ImmutableMap.<String, Object>of(
"localId", ImmutableList.of(uid));
GetAccountInfoResponse response;
Expand All @@ -84,10 +86,10 @@ User getUserById(String uid, String token) throws FirebaseAuthException {
throw new FirebaseAuthException(USER_NOT_FOUND_ERROR,
"No user record found for the provided user ID: " + uid);
}
return new User(response.getUsers().get(0));
return new UserRecord(response.getUsers().get(0));
}

User getUserByEmail(String email, String token) throws FirebaseAuthException {
UserRecord getUserByEmail(String email, String token) throws FirebaseAuthException {
final Map<String, Object> payload = ImmutableMap.<String, Object>of(
"email", ImmutableList.of(email));
GetAccountInfoResponse response;
Expand All @@ -102,13 +104,13 @@ User getUserByEmail(String email, String token) throws FirebaseAuthException {
throw new FirebaseAuthException(USER_NOT_FOUND_ERROR,
"No user record found for the provided email: " + email);
}
return new User(response.getUsers().get(0));
return new UserRecord(response.getUsers().get(0));
}

String createUser(User.Builder builder, String token) throws FirebaseAuthException {
String createUser(CreateRequest request, String token) throws FirebaseAuthException {
GenericJson response;
try {
response = post("signupNewUser", token, builder.build(), GenericJson.class);
response = post("signupNewUser", token, request.getProperties(), GenericJson.class);
} catch (IOException e) {
throw new FirebaseAuthException(USER_CREATE_ERROR,
"IO error while creating user account", e);
Expand All @@ -123,18 +125,18 @@ String createUser(User.Builder builder, String token) throws FirebaseAuthExcepti
throw new FirebaseAuthException(USER_CREATE_ERROR, "Failed to create new user");
}

void updateUser(User.Updater updater, String token) throws FirebaseAuthException {
void updateUser(UpdateRequest request, String token) throws FirebaseAuthException {
GenericJson response;
try {
response = post("setAccountInfo", token, updater.update(), GenericJson.class);
response = post("setAccountInfo", token, request.getProperties(), GenericJson.class);
} catch (IOException e) {
throw new FirebaseAuthException(USER_UPDATE_ERROR,
"IO error while updating user: " + updater.getUid(), e);
"IO error while updating user: " + request.getUid(), e);
}

if (response == null || !updater.getUid().equals(response.get("localId"))) {
if (response == null || !request.getUid().equals(response.get("localId"))) {
throw new FirebaseAuthException(USER_UPDATE_ERROR,
"Failed to update user: " + updater.getUid());
"Failed to update user: " + request.getUid());
}
}

Expand Down
32 changes: 1 addition & 31 deletions src/main/java/com/google/firebase/auth/ProviderUserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package com.google.firebase.auth;

import com.google.firebase.auth.internal.GetAccountInfoResponse;
import com.google.firebase.internal.Nullable;

/**
* Contains metadata regarding how a user is known by a particular identity provider (IdP).
* Instances of this class are immutable and thread safe.
*/
public class ProviderUserInfo {
class ProviderUserInfo implements UserInfo {

private final String uid;
private final String displayName;
Expand All @@ -39,51 +38,22 @@ public class ProviderUserInfo {
this.providerId = response.getProviderId();
}

/**
* Returns the user's unique ID assigned by the identity provider.
*
* @return a user ID string.
*/
public String getUid() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be annotated with @ Override?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return uid;
}

/**
* Returns the user's display name.
*
* @return a display name string or null.
*/
@Nullable
public String getDisplayName() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I think we still want @ Nullable annotation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return displayName;
}

/**
* Returns the user's email address.
*
* @return an email address string or null.
*/
@Nullable
public String getEmail() {
return email;
}

/**
* Returns the user's photo URL.
*
* @return a URL string or null.
*/
@Nullable
public String getPhotoUrl() {
return photoUrl;
}

/**
* Returns the ID of the identity provider. This can be a short domain name (e.g. google.com) or
* the identifier of an OpenID identity provider.
*
* @return an ID string that uniquely identifies the identity provider.
*/
public String getProviderId() {
return providerId;
}
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/google/firebase/auth/UserInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.auth;

import com.google.firebase.internal.Nullable;

/**
* A collection of standard profile information for a user. Used to expose profile information
* returned by an identity provider.
*/
public interface UserInfo {

/**
* Returns the user's unique ID assigned by the identity provider.
*
* @return a user ID string.
*/
String getUid();

/**
* Returns the user's display name, if available.
*
* @return a display name string or null.
*/
@Nullable
String getDisplayName();

/**
* Returns the user's email address, if available.
*
* @return an email address string or null.
*/
@Nullable
String getEmail();

/**
* Returns the user's photo URL, if available.
*
* @return a URL string or null.
*/
@Nullable
String getPhotoUrl();

/**
* Returns the ID of the identity provider. This can be a short domain name (e.g. google.com) or
* the identifier of an OpenID identity provider.
*
* @return an ID string that uniquely identifies the identity provider.
*/
String getProviderId();

}
Loading