Skip to content

Commit 3bdcd60

Browse files
committed
allow optional HTTP POST method override for logIn user method
1 parent 3d37485 commit 3bdcd60

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/ParseUser.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ class ParseUser extends ParseObject {
439439
* @return {Promise} A promise that is fulfilled with the user when
440440
* the login is complete.
441441
*/
442-
logIn(options?: FullOptions): Promise<ParseUser> {
442+
logIn(options?: FullOptions, usePost?: boolean): Promise<ParseUser> {
443443
options = options || {};
444444

445445
const loginOptions = {};
@@ -451,7 +451,7 @@ class ParseUser extends ParseObject {
451451
}
452452

453453
const controller = CoreManager.getUserController();
454-
return controller.logIn(this, loginOptions);
454+
return controller.logIn(this, loginOptions, usePost);
455455
}
456456

457457
/**
@@ -628,11 +628,12 @@ class ParseUser extends ParseObject {
628628
* @param {String} username The username (or email) to log in with.
629629
* @param {String} password The password to log in with.
630630
* @param {Object} options
631+
* @param {Boolean} usePost Override HTTP REST method and use POST instead of GET
631632
* @static
632633
* @return {Promise} A promise that is fulfilled with the user when
633634
* the login completes.
634635
*/
635-
static logIn(username: string, password: string, options?: FullOptions) {
636+
static logIn(username: string, password: string, options?: FullOptions, usePost?: boolean) {
636637
if (typeof username !== 'string') {
637638
return Promise.reject(
638639
new ParseError(
@@ -650,7 +651,7 @@ class ParseUser extends ParseObject {
650651
}
651652
const user = new this();
652653
user._finishFetch({ username: username, password: password });
653-
return user.logIn(options);
654+
return user.logIn(options, usePost);
654655
}
655656

656657
/**
@@ -1096,15 +1097,15 @@ const DefaultController = {
10961097
});
10971098
},
10981099

1099-
logIn(user: ParseUser, options: RequestOptions): Promise<ParseUser> {
1100+
logIn(user: ParseUser, options: RequestOptions, usePost?: boolean): Promise<ParseUser> {
11001101
const RESTController = CoreManager.getRESTController();
11011102
const stateController = CoreManager.getObjectStateController();
11021103
const auth = {
11031104
username: user.get('username'),
11041105
password: user.get('password')
11051106
};
11061107
return RESTController.request(
1107-
'GET', 'login', auth, options
1108+
usePost ? 'POST' : 'GET', 'login', auth, options
11081109
).then((response) => {
11091110
user._migrateId(response.objectId);
11101111
user._setExisted(true);

src/__tests__/ParseUser-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,39 @@ describe('ParseUser', () => {
229229
});
230230
});
231231

232+
it('can log in as a user with POST method', (done) => {
233+
ParseUser.enableUnsafeCurrentUser();
234+
ParseUser._clearCache();
235+
CoreManager.setRESTController({
236+
request(method, path, body) {
237+
expect(method).toBe('POST');
238+
expect(path).toBe('login');
239+
expect(body.username).toBe('username');
240+
expect(body.password).toBe('password');
241+
242+
return Promise.resolve({
243+
objectId: 'uid2',
244+
username: 'username',
245+
sessionToken: '123abc'
246+
}, 200);
247+
},
248+
ajax() {}
249+
});
250+
ParseUser.logIn('username', 'password', {}, true).then((u) => {
251+
expect(u.id).toBe('uid2');
252+
expect(u.getSessionToken()).toBe('123abc');
253+
expect(u.isCurrent()).toBe(true);
254+
expect(u.authenticated()).toBe(true);
255+
expect(ParseUser.current()).toBe(u);
256+
ParseUser._clearCache();
257+
const current = ParseUser.current();
258+
expect(current instanceof ParseUser).toBe(true);
259+
expect(current.id).toBe('uid2');
260+
expect(current.authenticated()).toBe(true);
261+
done();
262+
});
263+
});
264+
232265
it('fail login when invalid username or password is used', (done) => {
233266
ParseUser.enableUnsafeCurrentUser();
234267
ParseUser._clearCache();

0 commit comments

Comments
 (0)