parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

sessionToken migration for anonymous users

Open drorsun opened this issue 10 years ago • 10 comments

Hi, Somewhat late, because we had to migrate our app to our own hosted solution, and had problems with sessionToken we are now trying to migrate sessionTokens according to https://parse.com/tutorials/session-migration-tutorial and https://www.parse.com/docs/ios/guide#sessions-handling-invalid-session-token-error.

The problem we have is with anonymous (not logged in) users with invalid session token. With logged in users we understand that we should ask them to logout and login again when we catch invalid session tokens. But it is not clear what we should do with anonymous users. We added [PFUser enableRevocableSessionInBackground] after initialization, but the session token remains invalid and not being replaced with a revocable user session.

The question is what are we supposed to add for anonymous users.

Thanks, Dror

drorsun avatar May 26 '16 07:05 drorsun

Closing due to lack of activity, please update to latest parse-server version and reopen if the issue persist.

Don't forget to include your current:

  • node version
  • npm version
  • parse-server version
  • any relevant logs (VERBOSE=1 will enable verbose logging)

flovilmart avatar Jul 16 '16 01:07 flovilmart

I'm having exactly the same issue with the hosted version... My project is failing to proceed now because I pretty much get 209 invalid session tokens with any interaction I try and make with the parse backend now. The documentation seems to fairly clearly explain what needs to be done in the case where a full user is being used, but as @drorsun mentions, there is no explanation of what to do in the case of using anonymous users. An answer to this would be super-appreciated as my progress has dragged to a halt as this seems to be happening 100% of the time now (previously it was intermittent, and I could clear it by deleting the app, cleaning it, deleting derived data and closing xcode)

LiamInJapan avatar Jul 25 '16 09:07 LiamInJapan

We use anonymous users too and here's what we did, using the Unity client:

  1. While still on parse.com - call ParseUser.EnableRevocableSessionAsync() as early as possible in your code. This should migrate existing users (including anonymous) and ensure new users use revocable sessions
  2. When moved to parse-server/requiring revocable sessions on parse.com - if there is a session error, log out the user and create the user again using exactly the same id (we use device id as it ): https://www.parse.com/docs/rest/guide#users-anonymous-user-code-authdata-code- If the id matches then the existing user token will be received

steven-supersolid avatar Jul 25 '16 09:07 steven-supersolid

Hi @LiamInJapan Our solution is this -

  1. New fresh installations will always work with our hosted parse server
  2. On upgrades to existing installations we do this:
  • On the first run we work with the legacy parse server and run the session token migration (ParseUser.enableRevocableSessionInBackground()) and we wait for it to complete
  • If that migration succeeded then on the second and on any subsequent run we switch to work with our hosted parse server
  1. In the code we make sure that after January 2017 we will never try to work with the Legacy solution

drorsun avatar Jul 25 '16 11:07 drorsun

I think problem here is that parse.com Sessions are not migrated to hosted parse MongoDB. And that is pain in the back of the spine...

Our case:

  • Users login using gmail/live accounts
  • Old parse created one session on signUp and that session token stayed with the user forever
  • When we migrated, sessions got lost, and all users couldn't login again - because they already signed up
  • We couldn't reset password and call Parse.User.login(user.get("username", newPassword) and get new session token because new requirement was to allow users to login using username/password AND live AND google in parallel
  • So, our code checks if user has a valid session for the login provider, if he does, that session is returned, if not, we call Parse.User._linkWith(OAUTH_PROVIDER_AUTH_DATA) which links provider account with the Parse.User and logs him in, creating a sessionToken...

If parse.com would migrate ALL data, that would be perfect, users would have seamless transition. But no... :)

Or parse.com migrates all Sessions, but parse-server ignores them... If so, where are they stored? :|

cipiripper avatar Aug 03 '16 10:08 cipiripper

Ha. Legacy session tokens are stored in _User._session_token for each user. So, I moved all those to _Session with generated _id using mongo shell, and all tokens are now there. And my idea is that legacy app users targeting new parse-server which supports only _Session sessions (revokable ones) will authenticate without a problem with legacy session tokens. Because there is no check in Auth.js if sent token is a revokable one, only if it exists in _Session and is it expired.

Pseudo mongo db code for this:

var copySessions = function() {
    db.getCollection("_User").find().forEach(function(user){
        db.getCollection("_Session").insert({
            "_id": "GENERATED_10CHAR_ID",
            "sessionToken": user._session_token,
            "_p_user": "_User$"+user._id,
            "restricted" : false,
             ...         
        });
    });
}

cipiripper avatar Aug 04 '16 09:08 cipiripper

Actually. Legacy apps can't target new parse-server... And because they target old parse, which targets our mongo db, in which users already have their old sessions, old apps will work without a problem. Dang it. So no problem here actually. :)

cipiripper avatar Aug 04 '16 10:08 cipiripper

@drorsun Hi, how do you know when ParseUser.enableRevocableSessionInBackground is complete?

gregplumbly avatar Aug 26 '16 08:08 gregplumbly

@gregplumbly To wait for completion synchronously you can use

ParseUser.enableRevocableSessionInBackground().waitForCompletion()

We eventually decided it is not critical for us to wait synchronously so we use code such as this:

ParseUser.enableRevocableSessionInBackground().onSuccess(new Continuation<Void, Object>() {
    @Override
    public Object then(Task<Void> task) throws Exception {
        // do something to remember this succeeded in next runs.
        return task.getResult();
    }
});

drorsun avatar Aug 28 '16 14:08 drorsun

@drorsun Thank you Sir!

gregplumbly avatar Aug 30 '16 10:08 gregplumbly