Skip to content

Commit d27918c

Browse files
committed
Fixed issue #64.
1 parent a6fe362 commit d27918c

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/user.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,40 @@
640640
return user.logIn(options);
641641
},
642642

643+
/**
644+
* Logs in a user with a session token. On success, this saves the session
645+
* to disk, so you can retrieve the currently logged in user using
646+
* <code>current</code>.
647+
*
648+
* <p>Calls options.success or options.error on completion.</p>
649+
*
650+
* @param {String} sessionToken The sessionToken to log in with.
651+
* @param {Object} options A Backbone-style options object.
652+
* @return {AV.Promise} A promise that is fulfilled with the user when
653+
* the login completes.
654+
*/
655+
become: function(sessionToken, options) {
656+
options = options || {};
657+
658+
var user = AV.Object._create("_User");
659+
return AV._request(
660+
"users",
661+
"me",
662+
null,
663+
"GET",
664+
{
665+
useMasterKey: options.useMasterKey,
666+
session_token: sessionToken
667+
}
668+
).then(function(resp, status, xhr) {
669+
var serverAttrs = user.parse(resp, status, xhr);
670+
user._finishFetch(serverAttrs);
671+
user._handleSaveResult(true);
672+
return user;
673+
674+
})._thenRunCallbacks(options, user);
675+
},
676+
643677
/**
644678
* Logs in a user with a mobile phone number and sms code sent by
645679
* AV.User.requestLoginSmsCode.On success, this

test/user.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,33 @@ describe("User",function(){
3030

3131
});
3232

33-
describe("UserSignin",function(){
33+
describe("User.logIn and User.become",function(){
3434
it("should login",function(done){
3535
AV.User.logIn(username, password, {
3636
success: function(user) {
3737
expect(user.get("username")).to.be(username);
38-
done();
38+
console.dir(user);
39+
AV.User.become(user._sessionToken, {
40+
success: function(theUser) {
41+
expect(theUser.get("username")).to.be(username);
42+
done();
43+
},
44+
error: function(err){
45+
throw err;
46+
}
47+
});
3948
// Do stuff after successful login.
4049
},
4150
error: function(user, error) {
42-
throw eror;
51+
throw error;
4352
// The login failed. Check error to see why.
4453
}
4554
});
4655

4756
});
4857
});
4958

59+
5060
describe("Current User",function(){
5161
it("should return current user",function(done){
5262

0 commit comments

Comments
 (0)