Parse-SDK-iOS-OSX icon indicating copy to clipboard operation
Parse-SDK-iOS-OSX copied to clipboard

Old Session token deleted after PFUser.login or PFUser.become

Open riccardoch opened this issue 5 years ago • 14 comments

I want to offer multiple accounts on my iOS app (on Android I've already achieved that). I know the library allows only one current user and for this reason I decided to store users' session token. Once the user wants to change the account I call PFUser.become and PFUser.loginWithUsername when the user wants to add a new one. The problem is that every time I call those functions the previous session token is deleted from Database, on Android this doesn't happen.

How to reproduce the issue:

  1. You need one account logged in.
  2. To add a new user call PFUser.loginWithUsername providing username/password, and store the session token of the previous user.
  3. Then try to login in the old user calling PFUser.become providing the stored session token; you should encounter an "Invalid session token" error, and if you check the Database you should verify that the token has been deleted.

riccardoch avatar Nov 03 '20 18:11 riccardoch

How and where are you storing the session token?

drdaz avatar Nov 04 '20 12:11 drdaz

Locally on the device (using UserDefaults), without Parse SDK. On Android I've created the same flow and it works.

riccardoch avatar Nov 04 '20 13:11 riccardoch

I also have the same issue. Its working fine in Android SDK.

nilkanth987 avatar Nov 04 '20 14:11 nilkanth987

Can either of you make a failing test for this?

drdaz avatar Nov 05 '20 10:11 drdaz

So I have written this code for testing

PFUser.logInWithUsername(inBackground: "user1", password: "user1", block: {user,e in
            if e==nil{
                print(PFUser.current()?.username)
                self.session1 = PFUser.current()?.sessionToken
                PFUser.logInWithUsername(inBackground: "user2", password: "user2", block: {user,e in
                    if e==nil{
                        print(PFUser.current()?.username)
                        self.session2 = PFUser.current()?.sessionToken
                        PFUser.become(inBackground: self.session1!, block: {user,e in
                            if e==nil{
                                print(PFUser.current()?.username)
                                PFUser.become(inBackground: self.session2!, block: {user,e in
                                    if e==nil{
                                        print(PFUser.current()?.username)
                                    }else{
                                        print("Error in become user2!!!")
                                    }
                                })
                            }else{
                                print("Error in become user1!!!")
                            }
                        })
                    }else{
                        print("Error in user2 login!!!")
                    }
                })
            }else{
                print("Error in user1 login!!!")
            }
        })

And the output is

Optional("user1")
Optional("user2")
2020-11-05 17:29:52.355499+0530 MultipleLoginTest[942:22112] [Error]: invalid session token (Code: 209, Version: 1.19.1)
Error in become user1!!!

nilkanth987 avatar Nov 05 '20 12:11 nilkanth987

Great @nilkanth987 my issue is exactly the same!

@drdaz Is it possible that the SDK makes a delete request for the previous token when a new user is logging in?

riccardoch avatar Nov 05 '20 13:11 riccardoch

@drdaz Is it possible that the SDK makes a delete request for the previous token when a new user is logging in?

@riccardoch I think you are close. I believe only 1 session token is stored in the Keychain when a user logs in. When a user logs out, the session token is deleted. If another user signs in, the previous session token is overridden as the SDK is probably only designed to support 1 user at a time.

cbaker6 avatar Nov 09 '20 02:11 cbaker6

@nilkanth987 the flow you mentioned will definitely cause the issue I mentioned above. I don't understand what you are trying to do, once you use PFUser.logInWithUsername it automatically saves the sessionToken, any other login or use of become with a different user will overwrite the sessionToken

cbaker6 avatar Nov 09 '20 02:11 cbaker6

I think what you want to do is logout of the currentUser and then signIn as the new user. This will properly establish the needed session token

cbaker6 avatar Nov 09 '20 02:11 cbaker6

@drdaz @cbaker6 can someone take a look at this issue and PR.

https://github.com/parse-community/Parse-SDK-iOS-OSX/issues/1566

dsp1589 avatar Nov 09 '20 07:11 dsp1589

@nilkanth987 the flow you mentioned will definitely cause the issue I mentioned above. I don't understand what you are trying to do, once you use PFUser.logInWithUsername it automatically saves the sessionToken, any other login or use of become with a different user will overwrite the sessionToken

I think @nilkanth987 wants to offer multi accounts support, so the user would be able to change the account without prompting username/password every time. The method PFUser.become is perfect, but it seems previous token is deleted once the user changes account (using that function). On Android I was able to offer that, on iOS, for the moment, I've solved using cloud code.

riccardoch avatar Nov 09 '20 07:11 riccardoch

@nilkanth987 the flow you mentioned will definitely cause the issue I mentioned above. I don't understand what you are trying to do, once you use PFUser.logInWithUsername it automatically saves the sessionToken, any other login or use of become with a different user will overwrite the sessionToken

I think @nilkanth987 wants to offer multi accounts support, so the user would be able to change the account without prompting username/password every time. The method PFUser.become is perfect, but it seems previous token is deleted once the user changes account (using that function). On Android I was able to offer that, on iOS, for the moment, I've solved using cloud code.

Yup, that is exactly what I trying to do. I debugged and saw that the sessionToken for User1 is deleted after User2 logs in. As before PFUser.login of User2 it was available and in its callback the User1 session was deleted.

nilkanth987 avatar Nov 09 '20 10:11 nilkanth987

Do we know how the other Parse clients handle this? JS, .NET and whatever else we cover?

I can certainly see the use-case for this, but it would be good to know what our spec is supposed to be here.

drdaz avatar Nov 09 '20 10:11 drdaz

News about this issue?

riccardoch avatar Apr 15 '21 15:04 riccardoch