Re-authenticating User Credentials Swift
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)
Updated approach to Reauthenticate a user
let user = Auth.auth().currentUser
var credential: AuthCredential
user?.reauthenticateAndRetrieveData(with: credential, completion: {(authResult, error) in
if let error = error {
// An error happened.
}else{
// User re-authenticated.
}
})
You are getting error because you haven't initialize credential
You have to initialize AuthCredential first
These credential can vary from
EmailAuthCredential
FacebookAuthCredential
GithubAuthCredential
GoogleAuthCredential
PhoneAuthCredential
PlayGamesAuthCredential
TwitterAuthCredential
Initiziale the variable according to your authentication method e.g.
if you have chosen email you can use like
var credential: AuthCredential = EmailAuthProvider.credential(withEmail: "email", password: "pass")
Error reauthenticating user with Firebase / Xcode (for user email update)
I think you're missing the result parameter
let user = Auth.auth().currentUser
var credential: AuthCredential = EmailAuthProvider.credential(withEmail: "email", password: "pass")
user?.reauthenticate(with: credential) { result, error in
if let error = error {
// error handled here
} else {
// success
}
}
where result is an AuthDataResult that contains the result of a successful signin.
How to re-authenticate a user with firebase
You need to provide the actual credential:
let credential = EmailAuthProvider.credential(withEmail: email, password: password)
Getting an error trying to reauthenticate user with Firebase
You're creating your credential
object before the user has a chance to enter anything into the text fields:
var reauthEmailTextField = UITextField()
var reauthPasswordTextField = UITextField()
let credential: AuthCredential = EmailAuthProvider.credential(withEmail: reauthEmailTextField.text ?? "", password: reauthPasswordTextField.text ?? "") //<-- Here
In this scenario, you'll always get ""
for both values.
Instead, you need to capture these values inside your submit
closure:
let submit = UIAlertAction(title: "Submit", style: .default) { (sub) in
//get text field values and build `credential` object here
let credential: AuthCredential = EmailAuthProvider.credential(withEmail: reauthEmailTextField.text ?? "", password: reauthPasswordTextField.text ?? "")
user.reauthenticate(with: credential) { (result, error) in
guard error == nil else {
print("The user couldn't be reauthenticated. Email: \(reauthEmailTextField.text ?? "") Password: \(reauthPasswordTextField.text ?? "")")
return
} ...
How do I grab credential values for re authentication? (Question updated)
If you are asking why you can't get the email or password after you set FIREmailPasswordAuthProvider
, it is because it is set only. There is no function to retrieve email/password after you set FIREmailPasswordAuthProvider
. Firebase does not have a way to retrieve set passwords from their Auth
class. The user email can be retrieved with let userEmail = Auth.auth().currentUser?.email
You may need to redesign you code to allow the app to save passwords locally using NSUserDefaults
or simply make the user reenter them.
Related Topics
Fbsopenapplicationerrordomain Code =4 Error
Swift Framework: Umbrella Header '[...].H' Not Found
Avcapturevideopreviewlayer Orientation - Need Landscape
How to Calculate the Age Based on Nsdate
How to Change Uibutton Title Color
How to Set Image in Circle in Swift
Stretch Background Image for Uibutton
How to Animate Tab Bar Tab Switch with a Crossdissolve Slide Transition
The Identity Used to Sign the Executable Is No Longer Valid
How to Create a Release Build in Xcode
Xcode 4.2 Jumps to Main.M Every Time After Stopping Simulator
Getting Device Id or MAC Address in iOS
Uicollectionview Horizontal Paging Not Centered
Detecting If a User Is Moving in a Car
Can Siri Be Invoked Programmatically Within My App Using Private APIs