Skip to content

Commit 2bad411

Browse files
authored
feat: Login with username, password and additional authentication data via ParseUser.logInWithAdditionalAuth (#1955)
1 parent c03f72d commit 2bad411

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/ParseUser.js

+29
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,34 @@ class ParseUser extends ParseObject {
653653
return user.logIn(options);
654654
}
655655

656+
/**
657+
* Logs in a user with a username (or email) and password, and authData. On success, this
658+
* saves the session to disk, so you can retrieve the currently logged in
659+
* user using <code>current</code>.
660+
*
661+
* @param {string} username The username (or email) to log in with.
662+
* @param {string} password The password to log in with.
663+
* @param {object} authData The authData to log in with.
664+
* @param {object} options
665+
* @static
666+
* @returns {Promise} A promise that is fulfilled with the user when
667+
* the login completes.
668+
*/
669+
static logInWithAdditionalAuth(username: string, password: string, authData: AuthData, options?: FullOptions) {
670+
if (typeof username !== 'string') {
671+
return Promise.reject(new ParseError(ParseError.OTHER_CAUSE, 'Username must be a string.'));
672+
}
673+
if (typeof password !== 'string') {
674+
return Promise.reject(new ParseError(ParseError.OTHER_CAUSE, 'Password must be a string.'));
675+
}
676+
if (Object.prototype.toString.call(authData) !== '[object Object]') {
677+
return Promise.reject(new ParseError(ParseError.OTHER_CAUSE, 'Auth must be an object.'));
678+
}
679+
const user = new this();
680+
user._finishFetch({ username: username, password: password, authData });
681+
return user.logIn(options);
682+
}
683+
656684
/**
657685
* Logs in a user with an objectId. On success, this saves the session
658686
* to disk, so you can retrieve the currently logged in user using
@@ -1098,6 +1126,7 @@ const DefaultController = {
10981126
const auth = {
10991127
username: user.get('username'),
11001128
password: user.get('password'),
1129+
authData: user.get('authData'),
11011130
};
11021131
return RESTController.request(options.usePost ? 'POST' : 'GET', 'login', auth, options).then(
11031132
response => {

src/__tests__/ParseUser-test.js

+47
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,53 @@ describe('ParseUser', () => {
326326
});
327327
});
328328

329+
describe('loginWithAdditional', () => {
330+
it('loginWithAdditonal fails with invalid payload', async () => {
331+
ParseUser.enableUnsafeCurrentUser();
332+
ParseUser._clearCache();
333+
CoreManager.setRESTController({
334+
request(method, path, body) {
335+
expect(method).toBe('POST');
336+
expect(path).toBe('login');
337+
expect(body.username).toBe('username');
338+
expect(body.password).toBe('password');
339+
expect(body.authData).toEqual({ mfa: { key: '1234' } });
340+
341+
return Promise.resolve(
342+
{
343+
objectId: 'uid2',
344+
username: 'username',
345+
sessionToken: '123abc',
346+
authDataResponse: {
347+
mfa: { enabled: true },
348+
},
349+
},
350+
200
351+
);
352+
},
353+
ajax() {},
354+
});
355+
const response = await ParseUser.logInWithAdditionalAuth('username', 'password', {mfa: {key:'1234'}});
356+
expect(response instanceof ParseUser).toBe(true);
357+
expect(response.get('authDataResponse')).toEqual({mfa: { enabled: true }});
358+
});
359+
360+
it('loginWithAdditonal fails with invalid payload', async () => {
361+
ParseUser.enableUnsafeCurrentUser();
362+
ParseUser._clearCache();
363+
await expect(ParseUser.logInWithAdditionalAuth({}, 'password', {})).rejects.toThrowError(
364+
new ParseError(ParseError.OTHER_CAUSE, 'Username must be a string.')
365+
);
366+
await expect(ParseUser.logInWithAdditionalAuth('username', {}, {})).rejects.toThrowError(
367+
new ParseError(ParseError.OTHER_CAUSE, 'Password must be a string.')
368+
);
369+
await expect(ParseUser.logInWithAdditionalAuth('username', 'password', '')).rejects.toThrowError(
370+
new ParseError(ParseError.OTHER_CAUSE, 'Auth must be an object.')
371+
);
372+
});
373+
});
374+
375+
329376
it('preserves changes when logging in', done => {
330377
ParseUser.enableUnsafeCurrentUser();
331378
ParseUser._clearCache();

0 commit comments

Comments
 (0)