Search code examples
iosparse-platformpfuser

Queries on public classes fail while user has invalid session


This is questionable behavior on Parse's part, but I need to find a workaround. I have discovered that once a user's session is invalidated, all future requests will fail until the session is valid again, including queries for data that is available to query publicly - without logging in.

This is biting me because my Sign Up screen requires the user select from some PFObjects that are queried from a publicly accessible class. I show a list of options, they tap on one, then I set that object on the new user object created when they tap Sign Up. The problem arises when their session is invalidated. I present the log in or sign up screen, but if they want to sign up they cannot, because the query for the information required in sign up fails with error Error Domain=Parse Code=209 "invalid session token" (despite the fact the user doesn't need to have an active session to access the data).

Therefore it is impossible for the user to create a new account after they logged into an account and said account had its session invalidated. This will occur in a few situations: the account session actually expires, the user logged in with Facebook and its session expired, or the user was deleted altogether (perhaps because they posted something inappropriate). In all cases I get the 209 invalid session error, and I need to support the ability to sign up at that time - especially the latter because it's impossible for them to sign into their account that was deleted.

What can be done in this situation?


Solution

  • You can force log out the user when you detect their session is invalid. This request will still report the 209 error, despite the fact it actually succeeds in logging the user out. Once the user is logged out you can query on the public classes.

    func sessionInvalidated() {
        //force log out, otherwise can't fetch info in sign up screen with an invalid session
        PFUser.logOutInBackgroundWithBlock({ (error: NSError?) -> Void in
            //still will get 209 invalid session, user is still logged out in this case, ignore
            if error == nil || error!.code == 209 {
                self.showWelcomeScreen()
            }
        })
    }
    

    Source: https://github.com/ParsePlatform/Parse-SDK-iOS-OSX/issues/671