M. Kremer
M. Kremer

Reputation: 679

Current User isn't nil after calling PFUser.logOut() parse, swift2

I want to build a simple LogOut Button with parse but the current user is not getting nil.

PFUser.logOut()
var currentUser = PFUser.currentUser() // this should be nil but it isn't :(
print(currentUser)

I also tried:

PFUser.logOutInBackground()
var currentUser = PFUser.currentUser() // this is also not nil :(

So when I print the currentUser it is not nil like it should be. It is:

Optional(<PFUser: 0x7f8d99dd2320 , objectId: new, localId: local_58d62becf7a6f1dc> {

})

So I think the app is creating a new user?!

Upvotes: 0

Views: 985

Answers (3)

dev27
dev27

Reputation: 605

Try commenting out the following line of code in your AppDelegate.swift file -

PFUser.enableAutomaticUser()

enableAutomaticUser() will log in an anonymous user once you call PFUser.logOut(), and the username for an anonymous user is nil.

Upvotes: 5

Narendra G
Narendra G

Reputation: 569

Set return type of the function(logout) as optional type. Only optionals has the capability of holding nil value. Here is the example that i want to tell you

func loggedOut() -> Int? {

    // check you conditions here ex:
  if score > 0 {
  return score 
  } else {
 return nil
  }
}

Upvotes: 0

Lamour
Lamour

Reputation: 3030

Check to see if we have a currentUser in the AppDelegate in the didFinishLaunchingWithOptions method

   let user = PFUser.currentUser()
      if user == nil
       {
           // present loginViewController
       }
      else
        {
           print("user is \(user?.username)")  // <--- who is currently 
          // show any viewcontroller 
        }

Your logout Button should send the user back to the loginViewController after Login out of your app. Example:

        @IBAction func Logout(sender:UIButton)
         {
            PFUser.Logout()
            let LoginViewController = storyboard.instantiateViewControllerWithIdentifier("storyBoardID") 
            self.presentViewController(LoginViewController, animated: true, completion: nil)
         }

So From the Login View Controller, they could use their credentials to sign in back into your app, then the currentUser won't return a nil

Upvotes: 0

Related Questions