Skip to content

Commit 2ec5a6f

Browse files
authored
Support setting user from JSON (hydrate) (#730)
* Support setting user from JSON * flow typing
1 parent a4ba856 commit 2ec5a6f

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/CoreManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ type UserController = {
122122
signUp: (user: ParseUser, attrs: AttributeMap, options: RequestOptions) => Promise;
123123
logIn: (user: ParseUser, options: RequestOptions) => Promise;
124124
become: (options: RequestOptions) => Promise;
125+
hydrate: (userJSON: AttributeMap) => Promise;
125126
logOut: () => Promise;
126127
requestPasswordReset: (email: string, options: RequestOptions) => Promise;
127128
updateUserOnDisk: (user: ParseUser) => Promise;

src/ParseUser.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,21 @@ class ParseUser extends ParseObject {
638638
return controller.become(becomeOptions);
639639
}
640640

641+
/**
642+
* Logs in a user with a session token. On success, this saves the session
643+
* to disk, so you can retrieve the currently logged in user using
644+
* <code>current</code>. If there is no session token the user will not logged in.
645+
*
646+
* @param {Object} userJSON The JSON map of the User's data
647+
* @static
648+
* @return {Promise} A promise that is fulfilled with the user when
649+
* the login completes.
650+
*/
651+
static hydrate(userJSON: AttributeMap) {
652+
const controller = CoreManager.getUserController();
653+
return controller.hydrate(userJSON);
654+
}
655+
641656
static logInWith(provider, options) {
642657
return ParseUser._logInWith(provider, options);
643658
}
@@ -954,6 +969,17 @@ const DefaultController = {
954969
});
955970
},
956971

972+
hydrate(userJSON: AttributeMap) {
973+
const user = new ParseUser();
974+
user._finishFetch(userJSON);
975+
user._setExisted(true);
976+
if (userJSON.sessionToken && canUseCurrentUser) {
977+
return DefaultController.setCurrentUser(user);
978+
} else {
979+
return Promise.resolve(user);
980+
}
981+
},
982+
957983
logOut(): Promise {
958984
return DefaultController.currentUserAsync().then((currentUser) => {
959985
const path = Storage.generatePath(CURRENT_USER_KEY);

src/__tests__/ParseUser-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,45 @@ describe('ParseUser', () => {
287287
});
288288
});
289289

290+
it('can hydrate a user with sessionToken in server environment', async () => {
291+
ParseUser.enableUnsafeCurrentUser();
292+
ParseUser._clearCache();
293+
const user = await ParseUser.hydrate({
294+
objectId: 'uid3',
295+
username: 'username',
296+
sessionToken: '123abc',
297+
});
298+
expect(user.id).toBe('uid3');
299+
expect(user.isCurrent()).toBe(true);
300+
expect(user.existed()).toBe(true);
301+
});
302+
303+
it('can hydrate a user with sessionToken in non server environment', async () => {
304+
ParseUser.disableUnsafeCurrentUser();
305+
ParseUser._clearCache();
306+
const user = await ParseUser.hydrate({
307+
objectId: 'uid3',
308+
username: 'username',
309+
sessionToken: '123abc',
310+
});
311+
expect(user.id).toBe('uid3');
312+
expect(user.isCurrent()).toBe(false);
313+
expect(user.existed()).toBe(true);
314+
});
315+
316+
it('can hydrate a user without sessionToken', async () => {
317+
ParseUser.enableUnsafeCurrentUser();
318+
ParseUser._clearCache();
319+
await ParseUser.logOut();
320+
const user = await ParseUser.hydrate({
321+
objectId: 'uid3',
322+
username: 'username',
323+
});
324+
expect(user.id).toBe('uid3');
325+
expect(user.isCurrent()).toBe(false);
326+
expect(user.existed()).toBe(true);
327+
});
328+
290329
it('can send a password reset request', () => {
291330
CoreManager.setRESTController({
292331
request(method, path, body) {

0 commit comments

Comments
 (0)