我的应用程序中有Facebook登录名,并且我正在圈子里奔波,试图让需要通过注册屏幕的新用户和应该直接进入应用程序的注册用户保持平衡。这是处理Facebook登录的功能(当点击按钮并获得Facebook授权时):
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { if error != nil { return } FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "name"]).start { (connection, result, err) in let accessToken = FBSDKAccessToken.current() guard let accessTokenString = accessToken?.tokenString else {return} let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString) FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in if error != nil { return } if(FBSDKAccessToken.current() != nil){ // logged in self.performSegue(withIdentifier: "loginToRooms", sender: nil) }else{ // not logged in self.performSegue(withIdentifier: "showSetupScreen", sender: nil) } }) if err != nil { return } } }
在FIRAuth.auth上面的方框中,我基本上说的是,一旦点击Facebook登录按钮并经过Facebook授权,如果用户具有访问令牌,则直接进入应用程序。否则,请转到注册屏幕,用户将在其中输入必要的信息。
FIRAuth.auth
我在中也有此功能viewDidLoad,因此当启动应用程序时,如果用户以前已登录,他们甚至都不会看到登录屏幕,他们将直接进入应用程序:
viewDidLoad
// When app is launched, bring user straight in if they're already authorized. Otherwise show the login screen. FIRAuth.auth()?.addStateDidChangeListener { auth, user in if let user = user { // User is signed in. Show home screen self.performSegue(withIdentifier: "loginToRooms", sender: nil) } else { // No User is signed in. Show user the login screen return } }
但是,我在测试过程中发现,如果我点击Facebook登录名并以新用户身份输入Facebook凭据,则会获得授权,然后直接发送到应用程序中,而无需通过注册屏幕。这会引起各种各样的问题。
对于那些将Facebook Login与Firebase结合使用的人,有什么好的方法来处理这种情况?我需要介绍几种情况:
谢谢你的帮助!
当用户单击FB登录按钮时:
->执行本机FB登录。(就像您现在正在做什么)
->执行Firebase身份验证。
->但此外,请检查FirDatabase中是否存在数据
FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "name"]).start { (connection, result, err) in let accessToken = FBSDKAccessToken.current() guard let accessTokenString = accessToken?.tokenString else {return} let credentials = FIRFacebookAuthProvider.credential(withAccessToken: accessTokenString) FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in if error != nil { return } self.checkDataExistsinfirDataBaaseForUID(user.uid){ loginStaus in if(loginStaus){ // logged in self.performSegue(withIdentifier: "loginToRooms", sender: nil) }else{ // not logged in self.performSegue(withIdentifier: "showSetupScreen", sender: nil) } } }) if err != nil { return } }
理想情况下,在设置页面之后,您必须在FirebaseDatabase中的节点用户-> uID下添加设置信息。因此,请检查此方法中是否存在任何此类节点。
func checkDataExistsinfirDataBaaseForUID(_ uid: String, completion: (result: Bool) -> Void) { let ref = FIRDatabase.database().reference().child("users").child(uid) ref.observeSingleEventOfType(.Value, withBlock: { (snapshot) in completion(snapshot.exists()) }) }
测试用例 :
满足。
在fb登录之后-> firbase身份验证-> Firdatabse节点检查->如果返回true->登录到应用程序
通过使用您的装饰过程, FIRAuth.auth()?.addStateDidChangeListener {}
FIRAuth.auth()?.addStateDidChangeListener {}