Skip to content

Commit bdc640b

Browse files
authored
Refactoring the User Management API (firebase#37)
* Defined UserInfo interface. * Renamed User to UserRecord * Renamed Updater to Update and Builder to NewUser * Correcting some comments * Changed NewUser to CreateRequest and Update to UpdateRequest * Renamed arg * Fixing some typos and adding annotations
1 parent 093c28e commit bdc640b

7 files changed

Lines changed: 279 additions & 217 deletions

File tree

src/main/java/com/google/firebase/auth/FirebaseAuth.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import com.google.firebase.FirebaseApp;
3131
import com.google.firebase.FirebaseException;
3232
import com.google.firebase.ImplFirebaseTrampolines;
33+
import com.google.firebase.auth.UserRecord.CreateRequest;
34+
import com.google.firebase.auth.UserRecord.UpdateRequest;
3335
import com.google.firebase.auth.internal.FirebaseTokenFactory;
3436
import com.google.firebase.auth.internal.FirebaseTokenVerifier;
3537
import com.google.firebase.internal.FirebaseService;
@@ -207,17 +209,17 @@ public FirebaseToken then(@NonNull Task<String> task) throws Exception {
207209
* Gets the user data corresponding to the specified user ID.
208210
*
209211
* @param uid A user ID string.
210-
* @return A {@link Task} which will complete successfully with a {@link User} instance.
212+
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance.
211213
* If an error occurs while retrieving user data or if the specified user ID does not exist,
212214
* the task fails with a FirebaseAuthException.
213215
* @throws IllegalArgumentException If the user ID string is null or empty.
214216
*/
215-
public Task<User> getUser(final String uid) {
217+
public Task<UserRecord> getUser(final String uid) {
216218
checkArgument(!Strings.isNullOrEmpty(uid), "uid must not be null or empty");
217219
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
218-
new Continuation<GetTokenResult, User>() {
220+
new Continuation<GetTokenResult, UserRecord>() {
219221
@Override
220-
public User then(Task<GetTokenResult> task) throws Exception {
222+
public UserRecord then(Task<GetTokenResult> task) throws Exception {
221223
return userManager.getUserById(uid, task.getResult().getToken());
222224
}
223225
});
@@ -227,61 +229,62 @@ public User then(Task<GetTokenResult> task) throws Exception {
227229
* Gets the user data corresponding to the specified user email.
228230
*
229231
* @param email A user email address string.
230-
* @return A {@link Task} which will complete successfully with a {@link User} instance.
232+
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance.
231233
* If an error occurs while retrieving user data or if the email address does not correspond
232234
* to a user, the task fails with a FirebaseAuthException.
233235
* @throws IllegalArgumentException If the email is null or empty.
234236
*/
235-
public Task<User> getUserByEmail(final String email) {
237+
public Task<UserRecord> getUserByEmail(final String email) {
236238
checkArgument(!Strings.isNullOrEmpty(email), "email must not be null or empty");
237239
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
238-
new Continuation<GetTokenResult, User>() {
240+
new Continuation<GetTokenResult, UserRecord>() {
239241
@Override
240-
public User then(Task<GetTokenResult> task) throws Exception {
242+
public UserRecord then(Task<GetTokenResult> task) throws Exception {
241243
return userManager.getUserByEmail(email, task.getResult().getToken());
242244
}
243245
});
244246
}
245247

246248
/**
247-
* Creates a new user account with the attributes contained in the specified {@link User.Builder}.
249+
* Creates a new user account with the attributes contained in the specified
250+
* {@link CreateRequest}.
248251
*
249-
* @param builder A non-null {@link User.Builder} instance.
250-
* @return A {@link Task} which will complete successfully with a {@link User} instance
252+
* @param request A non-null {@link CreateRequest} instance.
253+
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance
251254
* corresponding to the newly created account. If an error occurs while creating the user
252255
* account, the task fails with a FirebaseAuthException.
253-
* @throws NullPointerException if the provided builder is null.
256+
* @throws NullPointerException if the provided request is null.
254257
*/
255-
public Task<User> createUser(final User.Builder builder) {
256-
checkNotNull(builder, "builder must not be null");
258+
public Task<UserRecord> createUser(final CreateRequest request) {
259+
checkNotNull(request, "create request must not be null");
257260
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
258-
new Continuation<GetTokenResult, User>() {
261+
new Continuation<GetTokenResult, UserRecord>() {
259262
@Override
260-
public User then(Task<GetTokenResult> task) throws Exception {
261-
String uid = userManager.createUser(builder, task.getResult().getToken());
263+
public UserRecord then(Task<GetTokenResult> task) throws Exception {
264+
String uid = userManager.createUser(request, task.getResult().getToken());
262265
return userManager.getUserById(uid, task.getResult().getToken());
263266
}
264267
});
265268
}
266269

267270
/**
268271
* Updates an existing user account with the attributes contained in the specified
269-
* {@link User.Updater}.
272+
* {@link UpdateRequest}.
270273
*
271-
* @param updater A non-null {@link User.Updater} instance.
272-
* @return A {@link Task} which will complete successfully with a {@link User} instance
274+
* @param request A non-null {@link UpdateRequest} instance.
275+
* @return A {@link Task} which will complete successfully with a {@link UserRecord} instance
273276
* corresponding to the updated user account. If an error occurs while updating the user
274277
* account, the task fails with a FirebaseAuthException.
275-
* @throws NullPointerException if the provided updater is null.
278+
* @throws NullPointerException if the provided update request is null.
276279
*/
277-
public Task<User> updateUser(final User.Updater updater) {
278-
checkNotNull(updater, "updater must not be null");
280+
public Task<UserRecord> updateUser(final UpdateRequest request) {
281+
checkNotNull(request, "update request must not be null");
279282
return ImplFirebaseTrampolines.getToken(firebaseApp, false).continueWith(
280-
new Continuation<GetTokenResult, User>() {
283+
new Continuation<GetTokenResult, UserRecord>() {
281284
@Override
282-
public User then(Task<GetTokenResult> task) throws Exception {
283-
userManager.updateUser(updater, task.getResult().getToken());
284-
return userManager.getUserById(updater.getUid(), task.getResult().getToken());
285+
public UserRecord then(Task<GetTokenResult> task) throws Exception {
286+
userManager.updateUser(request, task.getResult().getToken());
287+
return userManager.getUserById(request.getUid(), task.getResult().getToken());
285288
}
286289
});
287290
}

src/main/java/com/google/firebase/auth/FirebaseUserManager.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import com.google.common.base.Strings;
3232
import com.google.common.collect.ImmutableList;
3333
import com.google.common.collect.ImmutableMap;
34+
import com.google.firebase.auth.UserRecord.CreateRequest;
35+
import com.google.firebase.auth.UserRecord.UpdateRequest;
3436
import com.google.firebase.auth.internal.GetAccountInfoResponse;
3537

3638
import java.io.IOException;
@@ -68,7 +70,7 @@ class FirebaseUserManager {
6870
this.requestFactory = transport.createRequestFactory();
6971
}
7072

71-
User getUserById(String uid, String token) throws FirebaseAuthException {
73+
UserRecord getUserById(String uid, String token) throws FirebaseAuthException {
7274
final Map<String, Object> payload = ImmutableMap.<String, Object>of(
7375
"localId", ImmutableList.of(uid));
7476
GetAccountInfoResponse response;
@@ -84,10 +86,10 @@ User getUserById(String uid, String token) throws FirebaseAuthException {
8486
throw new FirebaseAuthException(USER_NOT_FOUND_ERROR,
8587
"No user record found for the provided user ID: " + uid);
8688
}
87-
return new User(response.getUsers().get(0));
89+
return new UserRecord(response.getUsers().get(0));
8890
}
8991

90-
User getUserByEmail(String email, String token) throws FirebaseAuthException {
92+
UserRecord getUserByEmail(String email, String token) throws FirebaseAuthException {
9193
final Map<String, Object> payload = ImmutableMap.<String, Object>of(
9294
"email", ImmutableList.of(email));
9395
GetAccountInfoResponse response;
@@ -102,13 +104,13 @@ User getUserByEmail(String email, String token) throws FirebaseAuthException {
102104
throw new FirebaseAuthException(USER_NOT_FOUND_ERROR,
103105
"No user record found for the provided email: " + email);
104106
}
105-
return new User(response.getUsers().get(0));
107+
return new UserRecord(response.getUsers().get(0));
106108
}
107109

108-
String createUser(User.Builder builder, String token) throws FirebaseAuthException {
110+
String createUser(CreateRequest request, String token) throws FirebaseAuthException {
109111
GenericJson response;
110112
try {
111-
response = post("signupNewUser", token, builder.build(), GenericJson.class);
113+
response = post("signupNewUser", token, request.getProperties(), GenericJson.class);
112114
} catch (IOException e) {
113115
throw new FirebaseAuthException(USER_CREATE_ERROR,
114116
"IO error while creating user account", e);
@@ -123,18 +125,18 @@ String createUser(User.Builder builder, String token) throws FirebaseAuthExcepti
123125
throw new FirebaseAuthException(USER_CREATE_ERROR, "Failed to create new user");
124126
}
125127

126-
void updateUser(User.Updater updater, String token) throws FirebaseAuthException {
128+
void updateUser(UpdateRequest request, String token) throws FirebaseAuthException {
127129
GenericJson response;
128130
try {
129-
response = post("setAccountInfo", token, updater.update(), GenericJson.class);
131+
response = post("setAccountInfo", token, request.getProperties(), GenericJson.class);
130132
} catch (IOException e) {
131133
throw new FirebaseAuthException(USER_UPDATE_ERROR,
132-
"IO error while updating user: " + updater.getUid(), e);
134+
"IO error while updating user: " + request.getUid(), e);
133135
}
134136

135-
if (response == null || !updater.getUid().equals(response.get("localId"))) {
137+
if (response == null || !request.getUid().equals(response.get("localId"))) {
136138
throw new FirebaseAuthException(USER_UPDATE_ERROR,
137-
"Failed to update user: " + updater.getUid());
139+
"Failed to update user: " + request.getUid());
138140
}
139141
}
140142

src/main/java/com/google/firebase/auth/ProviderUserInfo.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Contains metadata regarding how a user is known by a particular identity provider (IdP).
2424
* Instances of this class are immutable and thread safe.
2525
*/
26-
public class ProviderUserInfo {
26+
class ProviderUserInfo implements UserInfo {
2727

2828
private final String uid;
2929
private final String displayName;
@@ -39,51 +39,30 @@ public class ProviderUserInfo {
3939
this.providerId = response.getProviderId();
4040
}
4141

42-
/**
43-
* Returns the user's unique ID assigned by the identity provider.
44-
*
45-
* @return a user ID string.
46-
*/
42+
@Override
4743
public String getUid() {
4844
return uid;
4945
}
5046

51-
/**
52-
* Returns the user's display name.
53-
*
54-
* @return a display name string or null.
55-
*/
5647
@Nullable
48+
@Override
5749
public String getDisplayName() {
5850
return displayName;
5951
}
6052

61-
/**
62-
* Returns the user's email address.
63-
*
64-
* @return an email address string or null.
65-
*/
6653
@Nullable
54+
@Override
6755
public String getEmail() {
6856
return email;
6957
}
7058

71-
/**
72-
* Returns the user's photo URL.
73-
*
74-
* @return a URL string or null.
75-
*/
7659
@Nullable
60+
@Override
7761
public String getPhotoUrl() {
7862
return photoUrl;
7963
}
8064

81-
/**
82-
* Returns the ID of the identity provider. This can be a short domain name (e.g. google.com) or
83-
* the identifier of an OpenID identity provider.
84-
*
85-
* @return an ID string that uniquely identifies the identity provider.
86-
*/
65+
@Override
8766
public String getProviderId() {
8867
return providerId;
8968
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.auth;
18+
19+
import com.google.firebase.internal.Nullable;
20+
21+
/**
22+
* A collection of standard profile information for a user. Used to expose profile information
23+
* returned by an identity provider.
24+
*/
25+
public interface UserInfo {
26+
27+
/**
28+
* Returns the user's unique ID assigned by the identity provider.
29+
*
30+
* @return a user ID string.
31+
*/
32+
String getUid();
33+
34+
/**
35+
* Returns the user's display name, if available.
36+
*
37+
* @return a display name string or null.
38+
*/
39+
@Nullable
40+
String getDisplayName();
41+
42+
/**
43+
* Returns the user's email address, if available.
44+
*
45+
* @return an email address string or null.
46+
*/
47+
@Nullable
48+
String getEmail();
49+
50+
/**
51+
* Returns the user's photo URL, if available.
52+
*
53+
* @return a URL string or null.
54+
*/
55+
@Nullable
56+
String getPhotoUrl();
57+
58+
/**
59+
* Returns the ID of the identity provider. This can be a short domain name (e.g. google.com) or
60+
* the identifier of an OpenID identity provider.
61+
*
62+
* @return an ID string that uniquely identifies the identity provider.
63+
*/
64+
String getProviderId();
65+
66+
}

0 commit comments

Comments
 (0)