3

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?

1 Answer 1

9
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") 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. In the example of initializing you gave, are “email” and “pass” placeholders? What do they do?
These credentials are used to reauthenticate the users. Can you please tell me how did you authenticate the user first time
Hmm.. I think what you mean is that I need to work on my workflow. When I create a user using Auth.auth().createUser(withEmail: emailAddress, password: userPassword), I can use textfields to provide the parameters. For reauthentication to work, do I need to re-request the password in app again? That isn't clear from the Firebase documentation. Is there also a notification sent to the user's email box informing them that their account has been signed into again? Do they have to click on a link in the mail?
The user is already created. We are just trying for re authentication. Like a normal login. So they shouldn't be getting verify email.
Thanks. I'd expected some sort of email to authenticate the account but indeed it is just a question of the user logging in again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.