Re-authenticating User Credentials Swift
Solution 1
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)
Solution 2
In Swift 4 and latest firebase 4 the names have changed a bit, but the principle still remains. For your convenience:
let eMail = EmailAuthProvider.credential(withEmail: "[email protected]", password: "somepassword")
let fb = FacebookAuthProvider.credential(withAccessToken: "xxx")
let g = GoogleAuthProvider.credential(withIDToken: "xxx", accessToken: "xxx")
...
Auth.auth().currentUser?.reauthenticate(with: eMail, completion: {
[weak self]
(error) in
...
})
Solution 3
Firebase's documentation is currently outdated. Here is the correct way to handle reauthenticate
.
let user = Auth.auth().currentUser
user?.reauthenticate(with: credential, completion: { (result, error) in
if let err = error {
//..read error message
} else {
//.. go on
}
})
![sBourne](https://i.stack.imgur.com/EnP7u.jpg?s=256&g=1)
sBourne
Junior software developer and entrepreneur. Also currently a student of Computer Engineering with a specialization in software engineering.
Updated on June 18, 2022Comments
-
sBourne about 2 years
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.
-
ayalcin over 5 yearslet authentication = user.authentication is missing, how can we get that HixField ?
-
HixField over 5 yearsSorry, don't understand your question. Can you be more specific?
-
ayalcin over 5 yearsI need to change email address for google accounts. I am using currentUser.updateEmail() function which requires currentUser.reauthenticate() However I can not get GIDSignIn.sharedInstance().currentUser.authentication to get IdToken and AccessToken. GIDSignIn.sharedInstance().currentUser returns nil. I am using FirebaseAuthUI for login & register. And my current user = Auth.auth().currentUser does not have authentication property
-
HixField over 5 yearsI am not using the firebaseui, sorry
-
Visal Sambo over 5 yearswhat about phone number reauthentication?
-
Clint almost 5 yearsHow can I get the email and password for the cred?
-
Clint almost 5 yearsWhat do I put into the email and password fields?
-
temp_ almost 5 years@Clint I think you are asking about
EmailAuthProvider
which you can uselet credential = EmailAuthProvider.credential(withEmail: "[email protected]", password: "myPassword")
. -
Clint almost 5 yearsyes I apologize that is correct. Am I suppose to actually enter something in the Email: “email” and password: “ password”?
-
temp_ almost 5 years@Clint If your app requires a login, this would be the user's email and password. How you save/get the user's info is up to you. Using
reauthenticate
assumes you already have authenticated the user in the past. So you should already have the email and password, otherwise you need to look into how to authenticate notreauthenticate
. -
Clint almost 5 yearsOh I see, So I already saved the users email to the database however I never save passwords.. So thats a problem. The only way for me to get the users current password for reauthentication is to save the users current password? ( I want to avoid having them enter their old password)
-
temp_ almost 5 years@Clint you may need ask the user for them again or start using
NSUserDefaults
to save user passwords locally. -
Clint almost 5 yearsDo you mind helping me with my question, you definitely know how re authentication works. I can’t link it since I’m on my phone. But if you visit my profile it’s my most recent question..
-
Shredder2794 about 4 yearsNote that as of 2020 it's slightly updated to:
var credential = EmailAuthProvider.credential(withEmail: email, password: password)
-
Pablo Sanchez Gomez over 3 years@Shredder2794 OMG Thanks so much!!! I have been looking for this for a long time! :)