简体   繁体   English

如何在shoudPerformSegueWithIdentifier和Firebase中对用户进行身份验证?

[英]How do I authenticate users in shoudPerformSegueWithIdentifier and Firebase?

I'm trying to create an authentication page in storyboard using IOS swift and Firebase. 我正在尝试使用IOS swift和Firebase在情节提要中创建身份验证页面。 Here is my storyboardsegue declaration: 这是我的Storyboardsegue声明: 在此处输入图片说明

Then I'm using shouldPerformSegueWithIdentifier to authenticate it. 然后,我正在使用shouldPerformSegueWithIdentifier进行身份验证。 However, I also what to log the user in Firebase so my code is like so: 但是,我还要在Firebase中登录用户,因此我的代码如下所示:

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
if db.authData != nil {

  return true

} else {

  let email = emailTextField.text
  let password = passwordTextField.text

  db.authUser(email, password: password, withCompletionBlock: {
    error, authData in
    if error != nil {
      print(error.description)
    } else {
      print("logged in")
    }
  })

  return false
}

} }

This code somewhat works but I would have to click the log in button twice because the first time logs in the user in Firebase, and the second time the db.authData is no longer nil because the user is already logged in so it returns true. 该代码有些奏效,但我必须单击两次登录按钮,因为第一次是在Firebase中登录用户,而第二次db.authData不再是nil,因为用户已经登录,因此返回true。 I don't want to have to click twice to log in, I just want to click once. 我不想单击两次以登录,我只想单击一次。 I can't just put return true or return false in the withCompletionBlock either because the block returns void. 我不能只将return truereturn false放到withCompletionBlock ,因为该块返回void。 How do I make this work? 我该如何工作?

Using current implementation you can't achieve this feature. 使用当前的实现,您将无法实现此功能。 You have two ways to do this: 您有两种方法可以做到这一点:

  1. Make the db.authUser() synchronous and return it's result 使db.authUser()同步并返回结果
  2. Instead of connecting the login button segue to next screen, add an IBAction method and implement the method like 无需将登录按钮segue连接到下一个屏幕,而是添加IBAction方法并实现类似

@IBAction func login(sender : AnyObject?)
{
   let email = emailTextField.text
   let password = passwordTextField.text

   db.authUser(email, password: password, withCompletionBlock: {
     error, authData in
       if error != nil
       {
          print(error.description)
       }
       else
       {
          // Navigate to next screen
          // Start perform segue here
       }
    })  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以及何时保存用户的APN令牌以进行firebase消息传递? - How and when do I save the users APN token for firebase messaging? 如何让Firebase添加新用户而不覆盖现有用户(iOS,Xcode 8)? - How do I get Firebase to add new users and not overwrite current users (iOS, Xcode 8)? Firebase:我可以使用Facebook的新帐户套件来验证应用用户吗? - Firebase: Can I use Facebook's new Account Kit to authenticate app users? 如何允许用户通过Firebase在存储的配置文件中输入其信息,而无需登录/注册? - How do I allow users to input their information in a stored profile through Firebase without making them log in/register? Firebase Swift:如何使用“用户”集合下的 UID 创建新集合? - Firebase Swift: How do I create a new collection using the UID under my “users” collection? AWS Cognito使用Firebase电子邮件和密码IOS对用户进行身份验证 - AWS Cognito Authenticate Users with Firebase Email And Password IOS Evernote iOS SDK - 如何使用令牌进行身份验证? - Evernote iOS SDK - How do I authenticate with a token? 如何在 Unity 中使用 Google 帐户对用户进行身份验证? - How to use a Google account to authenticate users in Unity? 如何验证我的故障转储到AWS Lambda? - How do I authenticate my crash dumps to AWS Lambda? 如何在Firebase Swift中实现用户之间的朋友关系? - How would I implement friends relationship between users in Firebase Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM