Swfit Firebase 身份验证用户在没有邮件的情况下登录

时间:2021-02-08 22:02:21

标签: ios swift firebase firebase-authentication

我刚刚在我的 Firebase 身份验证中看到了这一点:

enter image description here

怎么会这样?它到底是什么意思?邮件怎么可能是空的?这是我第一次看到这个。通常总是有邮件。问题是该用户实际上无法成为 authenticated。呼叫 Auth.auth.currenUser 对他来说是零。这是 Apple 的问题还是我遗漏了什么?

这是我设置使用 Apple 登录的方式:

func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
    if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
        guard let nonce = currentNonce else {
            fatalError("Invalid state: A login callback was received, but no login request was sent.")
            
        }
        guard let appleIDToken = appleIDCredential.identityToken else {
            print("Unable to fetch identity token")
            return
        }
        guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
            print("Unable to serialize token string from data: \(appleIDToken.debugDescription)")
            return
        }
        
        let credential = OAuthProvider.credential(withProviderID: "apple.com", idToken: idTokenString, rawNonce: nonce)
        
        guard appleIDCredential.email != nil else {
            // User already signed in with this appleId once
            Auth.auth().signIn(with: credential, completion: { (user, error) in
                
                DataHandler.checkIfAppleUserExists(uid: (user?.user.uid)!) { (exists) in
                    if exists { // user has firebase-profile -> login
                        if error != nil {
                            self.resetButton(button: self.appleButton, buttonTitle: "Continue with Apple".localized())
                            // show error popUp
                            Utilities.showErrorPopUp(labelContent: "Login failed", description: error!.localizedDescription)
                        } else {
                            
                            let uid = Auth.auth().currentUser!.uid
                            // set user status to logged-in
                            if let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) {
                                defaults.setIsLoggedIn(value: true)
                                defaults.setUid(uid: uid)
                                defaults.synchronize()
                            }
                            
                            self.transitionToHome()
                        }
                    } else { // user doesn't have firebase-profile -> create username
                        
                        self.logoAnimation.stop()
                        self.logoAnimation.removeFromSuperview()
                        self.appleButton.setTitle("Continue with Apple".localized(), for: .normal)
                        self.appleButton.isEnabled = true
                        
                        let usernameVC = self.storyboard?.instantiateViewController(withIdentifier: "UsernameVC") as! UserNameVC
                        usernameVC.credential = credential
                        usernameVC.signInOption = Constants.SignInMethod.APPLE_EXISTS
                        self.navigationController?.pushViewController(usernameVC, animated: true)
                    }
                    self.view.isUserInteractionEnabled = true
                }
                
            })
            return
        }
        
        self.logoAnimation.stop()
        self.logoAnimation.removeFromSuperview()
        self.appleButton.setTitle("Continue with Apple".localized(), for: .normal)
        self.appleButton.isEnabled = true
        
        // first time apple sign in
        let usernameVC = self.storyboard?.instantiateViewController(withIdentifier: "UsernameVC") as! UserNameVC
        usernameVC.credential = credential
        usernameVC.signInOption = Constants.SignInMethod.APPLE
        self.navigationController?.pushViewController(usernameVC, animated: true)
    }
}

0 个答案:

没有答案