Skip to content

Commit d9cc4d2

Browse files
authored
allow optional HTTP POST method override for logIn user method (#1229)
1 parent 3d37485 commit d9cc4d2

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

integration/test/ParseUserTest.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ describe('Parse User', () => {
109109
});
110110
});
111111

112+
it('can log in a user using POST method', (done) => {
113+
Parse.User.signUp('asdf', 'zxcv').then(() => {
114+
return Parse.User.logIn('asdf', 'zxcv', { usePost: true });
115+
}).then((user) => {
116+
assert.equal(user.get('username'), 'asdf');
117+
expect(user.existed()).toBe(true);
118+
done();
119+
});
120+
});
121+
112122
it('can login users with installationId', async () => {
113123
Parse.User.enableUnsafeCurrentUser();
114124
const currentInstallation = await Parse.CoreManager.getInstallationController().currentInstallationId();

src/ParseUser.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ class ParseUser extends ParseObject {
449449
if (options.hasOwnProperty('installationId')) {
450450
loginOptions.installationId = options.installationId;
451451
}
452+
if (options.hasOwnProperty('usePost')) {
453+
loginOptions.usePost = options.usePost;
454+
}
452455

453456
const controller = CoreManager.getUserController();
454457
return controller.logIn(this, loginOptions);
@@ -1104,7 +1107,7 @@ const DefaultController = {
11041107
password: user.get('password')
11051108
};
11061109
return RESTController.request(
1107-
'GET', 'login', auth, options
1110+
options.usePost ? 'POST' : 'GET', 'login', auth, options
11081111
).then((response) => {
11091112
user._migrateId(response.objectId);
11101113
user._setExisted(true);

src/RESTController.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type RequestOptions = {
2424
include?: any;
2525
progress?: any;
2626
context?: any;
27+
usePost?: boolean;
2728
};
2829

2930
export type FullOptions = {
@@ -33,6 +34,7 @@ export type FullOptions = {
3334
sessionToken?: string;
3435
installationId?: string;
3536
progress?: any;
37+
usePost?: boolean;
3638
};
3739

3840
let XHR = null;

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', { usePost: 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)