Firebase User re-authentication initialising error

2020-07-29 17:29发布

问题:

When any user wants to update his/her password from app, i want to update that password of same FCM user and for that i have tried below code which define in official doc.

here is the screenshot of that

Here is my code

func authenticateAndUpdateFCMUserPassword(strNewPassword: String) {
    let user = Auth.auth().currentUser
    var credential: AuthCredential

    // Prompt the user to re-provide their sign-in credentials
    user?.reauthenticate(with: credential) { error in
        if let error = error {
            // An error happened.
        } else {
            // User re-authenticated.
        }
    }

    Auth.auth().currentUser?.updatePassword(to: strNewPassword) { (error) in
        if error != nil {
            print("Error occur while updating password")
        }
        else {
            print("Password Updated Successfully")
        }
    }
}

But in above code i'm facing below error at user?.reauthenticate line

Cannot convert value of type '(_) -> ()' to expected argument type 'AuthDataResultCallback?' (aka 'Optional<(Optional, Optional) -> ()>')

so i re write that by below code

user?.reauthenticate(with: credential, completion: { (dataResult, errorr) in
    if errorr != nil {
        // An error happened.
    } else {
        // User re-authenticated.
    }
})

but in above code i'm facing below error

Variable 'credential' used before being initialized

credential is defined only once and never used before this

will anyone please let me know what i'm doing wrong?

回答1:

When I set my credential variable as below, the error went away for me:

var credential: AuthCredential = EmailAuthProvider.credential(withEmail: email, password: password)

This answer helped me: https://stackoverflow.com/a/52160137/9625899