【发布时间】:2017-02-02 23:11:56
【问题描述】:
目前我正在使用 Firebase 身份验证并处理了许多错误,例如错误的用户名、密码、空字段等,并且一切正常。但是,当我尝试向下一页添加 segue 并使用数据库中不存在的随机电子邮件时,Firebase 身份验证没有给我任何错误,只是将用户传递到下一页。当我删除 segue 并放置随机用户时,它会给我一个错误,找不到这样的用户,这是我试图通过 segue 实现的。请指教,可能是什么问题?
AuthService 文件
func login(email: String, password: String, onComplete: Completion?) {
FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: { (user, error) in
if error != nil {
self.handleFirebaseError(error: error! as NSError, onComplete: onComplete)
} else {
onComplete?(nil, user)
}
})
}
func handleFirebaseError(error: NSError, onComplete: Completion?) {
print(error.debugDescription)
if let errorCode = FIRAuthErrorCode(rawValue: error.code) {
switch (errorCode) {
case .errorCodeInvalidEmail:
onComplete?("Invalid email address", nil)
break
case .errorCodeWrongPassword:
onComplete?("Invalid password", nil)
break
case .errorCodeUserNotFound:
onComplete?("Can't find user", nil)
break
default:
onComplete?("There was a problem authentication. Try again", nil)
}
}
}
视图控制器
@IBAction func loginBtnTapped(_ sender: Any) {
if let email = emailField.text, let pass = passwordField.text , (email.characters.count > 0 && pass.characters.count > 0) {
AuthService.instance.login(email: email, password: pass, onComplete: { (errMsg, data) in
guard errMsg == nil else {
self.alert(title: "Error Authentication", errMsg: errMsg!)
return
}
})
} else {
self.alert(title: "Username and password required", errMsg: "You must enter both a username and a password")
}
}
【问题讨论】:
标签: ios swift firebase firebase-authentication