MayNotBe MayNotBe - 5 months ago 46
Javascript Question

Parse-server user.getSessionToken() not defined

The documentation on

states:


Returns the session token for this user, if the user has been logged in, or if it is the result of a query with the master key. Otherwise, returns undefined.


I'm using the JS SDK. When I create and save a new user with the master key, I can grab the session token in the callback with
user.getSessionToken
. That works fine.

When I query for a user with the master key, the user is returned and I try to get the session token the same way and it's undefined.

I've confirmed there is a valid session for that user but the result is always the same.

I'm trying to login the queried user.

The
become()
function requires a session token.

Any help would be appreciated.

Thanks!

Answer

So it turns out that creating and saving new user generates a session token and a person can use the getSessionToken() in the block after the save to grab that token:

//create newUser and save username/password

return newUser.save(null, {userMasterKey:true})
    .then( function(user) {
        var token = user.getSessionToken();
        //use that token
});

Querying for a user and trying to get the token however wont work:

userQuery.first({useMasterKey:true})
     .then(function(user) {
            if(user)
            {
               var token = user.getSessionToken();
               //token is null
            }

I'm doing SSO login and using the token from the SAML server as the Parse user username. I needed to login to Parse after my mobile client user logged into the SSO server and provide the client with the parse session token.

For existing parse users the solution was to query for the user, use the master key to change the password to a random super-duper password, use that new password to login, and then grab the session token and send it along.

Hopefully this helps someone.