Shane O'Seasnain
Shane O'Seasnain

Reputation: 3664

Updated approach to Reauthenticate a user

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?

Upvotes: 3

Views: 2885

Answers (1)

Sahil Manchanda
Sahil Manchanda

Reputation: 10012

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")

Upvotes: 9

Related Questions