Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-authenticating User Credentials Swift

I wish to re-authenticate a user prior to allowing them to change their login information. However, due to the recent Firebase update, I found the documentation rather unhelpful. Using this link I produced the following authenticateUser() function.

func authenticateUser()
{
    let user = FIRAuth.auth()?.currentUser
    var credential: FIRAuthCredential

    //prompt user to re-enter info

    user?.reauthenticateWithCredential(credential, completion: { (error) in
        if error != nil
        {
            self.displayAlertMessage("Error reauthenticating user")
        }
        else
        {
            //user reauthenticated successfully
        }
    })
}

However, I am unsure what to do with the credential variable of type FIRAuthCredential, in order to re-authenticate the user. The documentation for this class can be found here.

like image 922
sBourne Avatar asked Jul 07 '16 18:07

sBourne


1 Answers

Getting the FIRAuthCredential object depends on what provider you want to use to reauthenticate.

Email:

let credential = EmailAuthProvider.credential(withEmail: email, password: password)

Facebook:

let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.currentAccessToken().tokenString)

Twitter:

let credential = TwitterAuthProvider.credential(withToken: session.authToken, secret: session.authTokenSecret)

Google:

let authentication = user.authentication
let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken, accessToken: authentication.accessToken)
like image 120
adolfosrs Avatar answered Sep 18 '22 18:09

adolfosrs