3

After create the Firebase user successful by code in login form. I deleted this new user in Firebase console, quit the app. When open the app again, it still inform login success with the user already deleted by admin. Why is this, and how to know in the login form that user already deleted?

Refer to my below code of login form:

override func viewDidLoad() {
    super.viewDidLoad()

    // get the current user is by setting a listener on the Auth object:
    FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
        if user != nil {
            // User is signed in.
            print("start login success: " + (user?.email)! )
            //self.performSegue(withIdentifier: loginToList, sender: nil)
        } else {
            // No user is signed in.
            print("No user is signed in.")
        }

    }

    //get the currently signed-in user by using the currentUser property. If a user isn't signed in, currentUser is nil:
    if let curUser = FIRAuth.auth()?.currentUser {
        // User is signed in.
        print("start current user: " + curUser.email! )
    } else {
        // No current user is signed in.
        print("Currently, no user is signed in.")
    }
}
}

sample code from https://firebase.google.com/docs/auth/ios/manage-users

adjuremods
  • 2,938
  • 2
  • 12
  • 17
L.N.Vu
  • 369
  • 4
  • 8

1 Answers1

1

Because the user is still logged. The first step is logout, you can do it in appDelegate inside applicationDidEnterBackground function. Just call this:
try! FIRAuth.auth()!.signOut()

 func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

     try! FIRAuth.auth()!.signOut()
}

The second step is to do a new login ( if the user allowed to save the email and password in... userDefault.... for example) and get the new information about the login of the user inside applicationDidBecomeActive function.

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

  // Check if the User data exist

    let User = UserDefaults.standard

    guard let myEmail = User.object(forKey: "userName") as? String  else {

        print("No User")

        return
    }

    //Check if the user wants the automatic access

    guard User.object(forKey: "automaticLogIn") as! Bool else {

        print("Automatic LogIn disable")
        return
    }

    // Now you can to do the automatic login

    let password = User.object(forKey: "password") as! String

    FIRAuth.auth()?.signIn(withEmail: myEmail, password: password, completion: { (user, error) in

        if error == nil{

            print("logIn succesfull")

        }else{

            let typeError = FIRAuthErrorCode(rawValue: error!._code)!
            switch typeError {
            case .errorCodeUserNotFound:
                print("user not found")
            case .errorCodeWrongPassword:
                print("wrong password")
            default:
                break

            }
        }
    })


}

And now you can call FIRAuth.auth()!.addStateDidChangeListener(), where you want, for example in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

 //  Save the user data inside userDefault, you can do it where you want.... like a form inside an alertView

    let User = UserDefaults.standard
    User.set("userEmail...", forKey: "userName")
    User.set("UserPassword", forKey: "password")
    User.set(true, forKey: "automaticLogIn")

FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
        if user != nil {
            // User is signed in.
            print("start login success: " + (user?.email)! )
            //self.performSegue(withIdentifier: loginToList, sender: nil)
        } else {
            // No user is signed in.
            print("No user is signed in.")
        }

    }

}
Rob
  • 2,649
  • 3
  • 27
  • 34
  • Thank you Rob, for your kind answer. I still wonder one more thing: if every time I sign out in the "func applicationDidEnterBackground". when open the app again, how can I get the current user? how can login automatic, and pass-through the Login form, if user already signed in? – L.N.Vu Nov 24 '16 at 02:33
  • 1
    @L.N.Vu I edited the code to explain well how can login automatic, and pass-through the Login form, if user already signed in. Look my edit inside func applicationDidBecomeActive and func viewDidLoad. – Rob Nov 24 '16 at 11:06
  • @ Rob thanks for your support, I will do test follow your instruction. Understand from your code that you store the last current user under the UserDefaults, and retrieve it to re-login Firebase when restart the app? Still I don't understand what is UserDefaults, and where it is stored, when quit and restart the app? It could not be store inside Firebase database, because before login again, can not read any data such the last user from database. – L.N.Vu Nov 24 '16 at 15:40
  • by the way, with my original question: "why the last current user still can login when it is already deleted from the Firebase Console?". I ask the Firebase support, they answer that "the session does not immediately expire upon account deletion and user can still stay authenticated up to 1 hour". Firebase advice me to refer to below topic for how to solve: http://stackoverflow.com/questions/19377172/firebase-authentication-not-revoked-when-user-deleted – L.N.Vu Nov 24 '16 at 15:48
  • 1
    @L.N.Vu Here you can find all explanation about UserDefault: https://developer.apple.com/reference/foundation/userdefaults – Rob Nov 24 '16 at 15:51