Re-authenticating User Credentials Swift

后端 未结 3 1833
清酒与你
清酒与你 2020-12-13 11:05

I wish to re-authenticate a user prior to allowing them to change their login information. However, due to the recent Firebase update, I found the documentation rather unhel

相关标签:
3条回答
  • 2020-12-13 11:30

    Getting the FIRAuthCredential object depends on what provider you want to use to reauthenticate.

    Email:

    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)
    

    Google:

    let authentication = user.authentication
    let credential = FIRGoogleAuthProvider.credentialWithIDToken(authentication.idToken, accessToken: authentication.accessToken)
    
    0 讨论(0)
  • 2020-12-13 11:33

    In Swift 4 and latest firebase 4 the names have changed a bit, but the principle still remains. For your convenience:

        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
            ...
        })
    
    0 讨论(0)
  • 2020-12-13 11:46

    Firebase's documentation is currently outdated. Here is the correct way to handle reauthenticate.

    let user = Auth.auth().currentUser
    
    user?.reauthenticate(with: credential, completion: { (result, error) in
       if let err = error {
          //..read error message             
       } else {
          //.. go on              
       }
    })
    
    0 讨论(0)
提交回复
热议问题