sBourne sBourne - 5 months ago 103
iOS Question

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.

Answer

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

Email:

let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

Facebook:

let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)

Twitter:

let credential = FIRTwitterAuthProvider.credentialWithToken(session.authToken, secret: session.authTokenSecret)

Google:

let authentication = user.authentication
let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken, accessToken: authentication.accessToken)