Skip to content

User subclass for loginWith, hydrate, me #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions integration/test/ParseUserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,43 @@ describe('Parse User', () => {
expect(user.doSomething()).toBe(5);
});

it('can get user (me) with subclass static', async () => {
Parse.User.enableUnsafeCurrentUser();

let user = await CustomUser.signUp('username', 'password');
const token = user.getSessionToken();

user = await CustomUser.me(token)
expect(user instanceof CustomUser).toBe(true);
expect(user.doSomething()).toBe(5);
});

it('can get hydrate user with subclass static', async () => {
Parse.User.enableUnsafeCurrentUser();

const user = await CustomUser.hydrate({
objectId: 'uid3',
username: 'username',
sessionToken: '123abc',
});

expect(user instanceof CustomUser).toBe(true);
expect(user.doSomething()).toBe(5);
});

it('can loginWith subclass static', async () => {
Parse.User.enableUnsafeCurrentUser();

let user = new CustomUser();
user.setUsername('Alice');
user.setPassword('sekrit');
await user.signUp();
user = await CustomUser.logInWith(provider.getAuthType(), provider.getAuthData());
expect(user._isLinked(provider)).toBe(true);
expect(user instanceof CustomUser).toBe(true);
expect(user.doSomething()).toBe(5);
});

it('can link without master key', async () => {
Parse.User.enableUnsafeCurrentUser();

Expand Down
16 changes: 8 additions & 8 deletions src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@ class ParseUser extends ParseObject {
if (options.useMasterKey) {
meOptions.useMasterKey = options.useMasterKey;
}
return controller.me(meOptions);
const user = new this();
return controller.me(user, meOptions);
}

/**
Expand All @@ -702,15 +703,16 @@ class ParseUser extends ParseObject {
*/
static hydrate(userJSON: AttributeMap) {
const controller = CoreManager.getUserController();
return controller.hydrate(userJSON);
const user = new this();
return controller.hydrate(user, userJSON);
}

/**
* Static version of {@link https://parseplatform.org/Parse-SDK-JS/api/master/Parse.User.html#linkWith linkWith}
* @static
*/
static logInWith(provider: any, options: { authData?: AuthData }, saveOpts?: FullOptions) {
const user = new ParseUser();
const user = new this();
return user.linkWith(provider, options, saveOpts);
}

Expand Down Expand Up @@ -845,7 +847,7 @@ class ParseUser extends ParseObject {
* @static
*/
static _logInWith(provider: any, options: { authData?: AuthData }, saveOpts?: FullOptions) {
const user = new ParseUser();
const user = new this();
return user.linkWith(provider, options, saveOpts);
}

Expand Down Expand Up @@ -1040,8 +1042,7 @@ const DefaultController = {
});
},

hydrate(userJSON: AttributeMap): Promise<ParseUser> {
const user = new ParseUser();
hydrate(user: ParseUser, userJSON: AttributeMap): Promise<ParseUser> {
user._finishFetch(userJSON);
user._setExisted(true);
if (userJSON.sessionToken && canUseCurrentUser) {
Expand All @@ -1051,12 +1052,11 @@ const DefaultController = {
}
},

me(options: RequestOptions): Promise<ParseUser> {
me(user: ParseUser, options: RequestOptions): Promise<ParseUser> {
const RESTController = CoreManager.getRESTController();
return RESTController.request(
'GET', 'users/me', {}, options
).then((response) => {
const user = new ParseUser();
user._finishFetch(response);
user._setExisted(true);
return user;
Expand Down