[英]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 true
或return 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: 您有两种方法可以做到这一点:
db.authUser()
synchronous and return it's result 使db.authUser()
同步并返回结果 @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.