Search code examples
swiftfirebasefirebase-authenticationcredentials

Cannot re auth user to change email or reset password (Firebase.. Swift)


I cannot comment on answers so I have to ask this question, I apologize.

I am getting an error on Xcode when trying to re authenticate the user so their email and or password is changed.

Here is the code I've written.


    var credential: AuthCredential

    let user = Auth.auth().currentUser
            // re authenticate the user
        user?.reauthenticate(with: credential) { error in
            if let error = error {
                // An error happened.
            } else {
                // User re-authenticated.
            }
        }


        }

I get an error that states Cannot convert value of type '(_) -> ()' to expected argument type 'AuthDataResultCallback?' (aka 'Optional<(Optional, Optional) -> ()>')


Solution

  • It looks like the reauthenticate function returns a tuple, so you have to change the code:

    var credential: AuthCredential
    
    let user = Auth.auth().currentUser
    
    // re authenticate the user
    user?.reauthenticate(with: credential) { (authDataResult, error) in
        if let error = error {
            // An error happened.
        } else {
            // Use `user` property of result to check is successful.
        }
    }
    

    AuthDataResultCallback

    FIRAuthDataResult