Updated approach to Reauthenticate a user

不问归期 提交于 2020-01-03 19:58:12

问题


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:


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


来源:https://stackoverflow.com/questions/52159866/updated-approach-to-reauthenticate-a-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!