I've been looking in the Firebase Documentation and found the method to reauthenticate a user shown below.
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.
}
}
I get an error and a warning though from the compiler as follows:
Error:
Variable 'credential' used before being initialized
Warning:
'reauthenticate(with:completion:)' is deprecated: Please use
reauthenticateAndRetrieveDataWithCredential:completion: for
Objective-C or
reauthenticateAndRetrieveData(WithCredential:completion:) for Swift
instead.
Has anyone an example of an updated method?
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")
Related
So, I tried to add a delete account function using Firebase Manage User service here, it's showing me error. Below is the code.
#IBAction func deleteAccount() {
let user = Auth.auth().currentUser
var credential: AuthCredential
user?.reauthenticate(with: credential) { error in
if let error = error {
self.redAlert(message: "Unable to authenticate. Please try again.")
} else {
Auth.auth().currentUser?.delete { error in
if let error = error {
self.redAlert(message: "An error happened. Please contact support to delete your account")
} else {
self.greenAlert(message: "Your account has been deleted")
}
}
}
}
Attached the screenshot as well.
It keeps showing "Contextual closure type '(AuthDataResult?, Error?) -> Void' expects 2 arguments, but 1 was used in closure body" which I really don't know how to fix it. Would you mind helping to pinpoint this?
Screenshot
EDIT: I actually found the solution. The right code should be:
#IBAction func deleteAccount(_send: Any) {
let user = Auth.auth().currentUser
let emailText: String = email.text!
let passwordText: String = password.text!
var credential: AuthCredential = EmailAuthProvider.credential(withEmail: emailText, password: passwordText)
user?.reauthenticate(with: credential, completion: { (result, error) in
if let error = error {
self.redAlert(message: "Unable to authenticate. Please try again.")
} else {
Auth.auth().currentUser?.delete { error in
if let error = error {
self.redAlert(message: "An error happened. Please contact support to delete your account")
} else {
self.greenAlert(message: "Your account has been deleted")
self.stopLoading()
self.logout()
}
}
}
})
}
Hope it helps anyone that is struggling!
I cannot comment on answers so I have to ask this question, I apologize.
I am getting an error on Xcode when trying to re authenticate the user so their email and or password is changed.
Here is the code I've written.
var credential: AuthCredential
let user = Auth.auth().currentUser
// re authenticate the user
user?.reauthenticate(with: credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}
}
I get an error that states Cannot convert value of type '(_) -> ()' to expected argument type 'AuthDataResultCallback?' (aka 'Optional<(Optional, Optional) -> ()>')
It looks like the reauthenticate function returns a tuple, so you have to change the code:
var credential: AuthCredential
let user = Auth.auth().currentUser
// re authenticate the user
user?.reauthenticate(with: credential) { (authDataResult, error) in
if let error = error {
// An error happened.
} else {
// Use `user` property of result to check is successful.
}
}
AuthDataResultCallback
FIRAuthDataResult
I'm trying to create an "update email" screen for an iOS app, using Xcode Version 10.2.1 and Firebase. All of the code for updating the email with Firebase works fine, but I'm getting the following error when I try to reauthenticate the user:
Cannot convert value of type '(_) -> ()' to expected argument type 'AuthDataResultCallback?' (aka 'Optional<(Optional, Optional) -> ()>')
Oh, and I'm using Email credentials to Auth users.
I've searched multiple threads here and scoured through the Firebase documentation and can't seem to figure out what is causing this error.
Here's my code, which I pulled from the Firebase Documentation, as well as this thread:
let user = Auth.auth().currentUser
var credential: AuthCredential = EmailAuthProvider.credential(withEmail: "email", password: "pass")
user?.reauthenticate(with: credential) { error in
if let error = error {
// error handled here
} else {
// success
}
}
The error is showing up on the "user?.reauthenticate(with: credential) { error in " line.
Anyone have any idea what is causing this?
(And, apologies if I'm missing something obvious. I'm relatively new to working with Swift/Xcode. )
THANKS!!
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.
I have built authentication into my app using Firebase/Facebook Auth and now I would like to build into my app a system for managing a user's presence (eg; updating a 'status' field on Firebase for the user to say 'online or offline') as described in the Firebase Docs here
*My question is... where do I put all this managing presense code?, here is my 'signIntoFirebase' function. would I create a separate function called 'manageUserPresense' and call it straight after 'firebaseLoginComplete'?
func signIntoFirebase(firebaseLoginComplete: #escaping (_ status: Bool, _ error: Error?) -> ()){
//Getting FB authentication
guard let authenticationToken = AccessToken.current?.authenticationToken else { return }
//Getting credential using authentication token
let credential = FacebookAuthProvider.credential(withAccessToken: authenticationToken)
//Signing in to Firebase using FB authentication token
Auth.auth().signIn(with: credential) { (user, error) in
if let error = error { //if there is an error
print (error)
firebaseLoginComplete(false, error)
return
}
firebaseLoginComplete(true, nil)
self.isLoggedIn = true
print ("Successfully authenticated into Firebase")
}
}//end func
I'm developing app with new firebase from google.
And I'm having problem with updating user email and password.
Here's what I've tried.
let currentUser = FIRAuth.auth()?.currentUser
currentUser?.updateEmail(email) { error in
if let error = error {
print(error)
} else {
// Email updated.
currentUser?.updatePassword(password) { error in
if let error = error {
} else {
// Password updated.
print("success")
}
}
}
}
But when updating password this occurs error like this.
"Domain=FIRAuthErrorDomain Code=17014 "This operation is sensitive and requires recent authentication..."
As I know we have to re-configure user after updating email.
I tried with this code for re-cofiguring from the firebase.
let user = FIRAuth.auth()?.currentUser
var credential: FIRAuthCredential
// Prompt the user to re-provide their sign-in credentials
user?.reauthenticateWithCredential(credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}
But this occurs error
Variable 'credential' used before being initialed
I know this is because I don't initialize 'credential' variable but I don't know how to fix this to work.
Is there anybody who knows solution?
In your edit you didn't initialize your FIRAuthCredential ... it should be var credential = FIRAuthCredential() or simply use it like below code
let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)
user?.reauthenticateWithCredential(credential) { error in
if let error = error {
// An error happened.
} else {
// User re-authenticated.
}
}