简体   繁体   中英

Failure to login straight after verifying but successful on second tap

I am creating an app that allow user sign up/login through Firebase API. The flow is that the user signs up a new account, is taken to an email verification page (the user is sent a verification email at this point), then they may login after verifying their account. The problem is, on the first time they tap "Login", the alert is shown that they still need to be verified (even after verifying) but when they tap again it will successfully log them in. I want the user to be able to login successfully first time straight after verifying. Thank you in advance for the help.

  private func login() {
    let userEmail = textFrom(loginEmail)
    let userPassword = textFrom(loginPassword)
    let authUser = Auth.auth().currentUser
    self.loginButton.loadingIndicator(show: true)
    guard let isVerified = authUser?.isEmailVerified else { return }

    Auth.auth().signIn(withEmail: userEmail, password: userPassword, completion: {(user, error) in
        if let firebaseError = error {
            self.loginButton.loadingIndicator(show: false)
            self.showError(firebaseError)
        }

        if authUser != nil && !isVerified {
            self.loginButton.loadingIndicator(show: false)
            self.presentAlert(withTitle: "Verify Email", message: "Please verify your email first before logging in")
            self.clearFields()
        } else {
            self.loginButton.loadingIndicator(show: false)
            self.transitionToHome()
        }
    })
}

Also, I know this isn't Github but I am new to using Firebase, if you think my logic can be improved (this function is called in the IBAction of the login button) then please let me know. Thank you:)

Are you positive that the alert you quoted is the one being returned? No other similarly worded alerts coming from somewhere else?

If so, then the only way it can be shown is if isVerified is false and authUser is not nil (which is redundant, because if it was nil, the guard let... optional chaining would have immediately returned from the function).

So it looks like authUser.isEmailVerified is returning false. You can verify this by adding a breakpoint at the line: if authUser != nil && !isVerified {

Without knowing more it's impossible to say what's going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM