重新验证用户凭据Swift

时间:2016-07-07 18:49:51

标签: ios swift authentication firebase firebase-authentication

我希望在允许用户更改其登录信息之前重新验证用户身份。但是,由于最近的Firebase更新,我发现文档相当无用。使用this link我生成了以下authenticateUser()函数。

func authenticateUser()
{
    let user = FIRAuth.auth()?.currentUser
    var credential: FIRAuthCredential

    //prompt user to re-enter info

    user?.reauthenticateWithCredential(credential, completion: { (error) in
        if error != nil
        {
            self.displayAlertMessage("Error reauthenticating user")
        }
        else
        {
            //user reauthenticated successfully
        }
    })
}

但是,我不确定如何处理FIRAuthCredential类型的凭证变量,以便重新验证用户身份。可以找到此课程的文档here

3 个答案:

答案 0 :(得分:24)

获取FIRAuthCredential对象取决于您要用于重新进行身份验证的提供程序。

电子邮件:

let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

Facebook的:

let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)

Twitter的:

let credential = FIRTwitterAuthProvider.credentialWithToken(session.authToken, secret: session.authTokenSecret)

谷歌:

let authentication = user.authentication
let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken, accessToken: authentication.accessToken)

答案 1 :(得分:4)

在Swift 4和最新的firebase 4中,名字有所改变,但原则仍然存在。为方便起见:

    let eMail = EmailAuthProvider.credential(withEmail: "some@email.com", password: "somepassword")
    let fb = FacebookAuthProvider.credential(withAccessToken: "xxx")
    let g = GoogleAuthProvider.credential(withIDToken: "xxx", accessToken: "xxx")
    ...

    Auth.auth().currentUser?.reauthenticate(with: eMail, completion: {
        [weak self]
        (error) in
        ...
    })

答案 2 :(得分:0)

Firebase的documentation当前已过时。这是处理reauthenticate的正确方法。

let user = Auth.auth().currentUser

user?.reauthenticate(with: credential, completion: { (result, error) in
   if let err = error {
      //..read error message             
   } else {
      //.. go on              
   }
})