Skip to content

feat: Add Parse.User.loginAs #1875

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 2 commits into from
Apr 28, 2023
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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"jsdoc/require-param-description": 0,
"jsdoc/require-property-description": 0,
"jsdoc/require-param-type": 0,
"jsdoc/tag-lines": 0,
"jsdoc/check-param-names": [
"error",
{
Expand Down
31 changes: 31 additions & 0 deletions integration/test/ParseUserTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,27 @@ describe('Parse User', () => {
expect(sessions[1].get('sessionToken')).toBe(installationUser.getSessionToken());
});

it('can login with userId', async () => {
Parse.User.enableUnsafeCurrentUser();

const user = await Parse.User.signUp('parsetest', 'parse', { code: 'red' });
assert.equal(Parse.User.current(), user);
await Parse.User.logOut();
assert(!Parse.User.current());

const newUser = await Parse.User.loginAs(user.id);
assert.equal(Parse.User.current(), newUser);
assert(newUser);
assert.equal(user.id, newUser.id);
assert.equal(user.get('code'), 'red');

await Parse.User.logOut();
assert(!Parse.User.current());
await expectAsync(Parse.User.loginAs('garbage')).toBeRejectedWithError(
'user not found'
);
});

it('can become a user', done => {
Parse.User.enableUnsafeCurrentUser();
let session = null;
Expand Down Expand Up @@ -782,6 +803,16 @@ describe('Parse User', () => {
expect(user.doSomething()).toBe(5);
});

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

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

user = await CustomUser.loginAs(user.id);
expect(user instanceof CustomUser).toBe(true);
expect(user.doSomething()).toBe(5);
});

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

Expand Down
31 changes: 31 additions & 0 deletions src/ParseUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,25 @@ class ParseUser extends ParseObject {
return user.logIn(options);
}

/**
* Logs in a user with an objectId. On success, this saves the session
* to disk, so you can retrieve the currently logged in user using
* <code>current</code>.
*
* @param {string} userId The objectId for the user.
* @static
* @returns {Promise} A promise that is fulfilled with the user when
* the login completes.
*/
static loginAs(userId: string) {
if (!userId) {
throw new ParseError(ParseError.USERNAME_MISSING, 'Cannot log in as user with an empty user id');
}
const controller = CoreManager.getUserController();
const user = new this();
return controller.loginAs(user, userId);
}

/**
* Logs in a user with a session token. On success, this saves the session
* to disk, so you can retrieve the currently logged in user using
Expand Down Expand Up @@ -1097,6 +1116,18 @@ const DefaultController = {
);
},

loginAs(user: ParseUser, userId: string): Promise<ParseUser> {
const RESTController = CoreManager.getRESTController();
return RESTController.request('POST', 'loginAs', { userId }, { useMasterKey: true }).then(response => {
user._finishFetch(response);
user._setExisted(true);
if (!canUseCurrentUser) {
return Promise.resolve(user);
}
return DefaultController.setCurrentUser(user);
});
},

become(user: ParseUser, options: RequestOptions): Promise<ParseUser> {
const RESTController = CoreManager.getRESTController();
return RESTController.request('GET', 'users/me', {}, options).then(response => {
Expand Down
67 changes: 67 additions & 0 deletions src/__tests__/ParseUser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,73 @@ describe('ParseUser', () => {
});
});

it('does not allow loginAs without id', (done) => {
try {
ParseUser.loginAs(null, null);
} catch (e) {
expect(e.message).toBe('Cannot log in as user with an empty user id')
done();
}
});

it('can login as a user with an objectId', async () => {
ParseUser.disableUnsafeCurrentUser();
ParseUser._clearCache();
CoreManager.setRESTController({
request(method, path, body, options) {
expect(method).toBe('POST');
expect(path).toBe('loginAs');
expect(body.userId).toBe('uid4');
expect(options.useMasterKey).toBe(true);

return Promise.resolve(
{
objectId: 'uid4',
username: 'username',
sessionToken: '123abc',
},
200
);
},
ajax() {},
});

const user = await ParseUser.loginAs('uid4');
expect(user.id).toBe('uid4');
expect(user.isCurrent()).toBe(false);
expect(user.existed()).toBe(true);
});

it('can loginAs a user with async storage', async () => {
const currentStorage = CoreManager.getStorageController();
CoreManager.setStorageController(mockAsyncStorage);
ParseUser.enableUnsafeCurrentUser();
ParseUser._clearCache();
CoreManager.setRESTController({
request(method, path, body, options) {
expect(method).toBe('POST');
expect(path).toBe('loginAs');
expect(body.userId).toBe('uid5');
expect(options.useMasterKey).toBe(true);
return Promise.resolve(
{
objectId: 'uid5',
username: 'username',
sessionToken: '123abc',
},
200
);
},
ajax() {},
});

const user = await ParseUser.loginAs('uid5');
expect(user.id).toBe('uid5');
expect(user.isCurrent()).toBe(true);
expect(user.existed()).toBe(true);
CoreManager.setStorageController(currentStorage);
});

it('can become a user with a session token', done => {
ParseUser.enableUnsafeCurrentUser();
ParseUser._clearCache();
Expand Down