Ios Firebase电子邮件验证Swift
我尝试使用Firebase身份验证,但在登录时允许我进入,即使我没有使用电子邮件确认验证。 我有两个Viewcontroller,一个用于登录,另一个用于注册。 我可以登录并收到电子邮件进行验证,但我也可以在未经验证的情况下登录Ios Firebase电子邮件验证Swift,ios,swift,firebase,firebase-authentication,Ios,Swift,Firebase,Firebase Authentication,我尝试使用Firebase身份验证,但在登录时允许我进入,即使我没有使用电子邮件确认验证。 我有两个Viewcontroller,一个用于登录,另一个用于注册。 我可以登录并收到电子邮件进行验证,但我也可以在未经验证的情况下登录 public func sendVerificationMail() { if self.authUser != nil && !self.authUser!.isEmailVerified { self.authUser!.sendEmai
public func sendVerificationMail() {
if self.authUser != nil && !self.authUser!.isEmailVerified {
self.authUser!.sendEmailVerification(completion: { (error) in
// Notify the user that the mail has sent or couldn't because of an error.
})
} else {
// Either the user is not available, or the user is already verified.
}
}
@IBAction func signupButtonTapped(_ sender: Any) {
print("Sign up button tapped")
Auth.auth().createUser(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) { (user, error) in
if user != nil {
print("User has Signed Up")
self.sendVerificationMail()
}
if error != nil {
print("User cant Sign Up")
}
}
}
@IBAction func signinButtonTapped(_ sender: Any) {
Auth.auth().signIn(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) { (user, error) in
if user != nil {
print("User has Signed In")
}
if error != nil {
print("Cant Sign in user")
} else {
self.performSegue(withIdentifier: "toHome", sender: nil)
}
}
}
Firebase Auth不会阻止未验证电子邮件的用户登录。如果要防止未经验证的用户前进,则需要在客户端上使用
isEmailVerified
布尔值对其进行编码
Auth.auth().signIn(withEmail: self.userEmailTextField.text!, password: self.userPasswordTextField.text!) { (authResult, error) in
if let authResult = authResult {
let user = authResult.user
print("User has Signed In")
if user.isEmailVerified {
self.performSegue(withIdentifier: "toHome", sender: nil)
} else {
// do whatever you want to do when user isn't verified
}
}
if let error = error {
print("Cant Sign in user")
}
}
您也可以尝试以下方法:
if( !Auth.auth().currentUser!.isEmailVerified) {
// do something
}
一个比@jen person提供的解决方案更全面的解决方案(并基于另一个SO答案)是:
final类mySignenView:UIView{//或mySignenView控制器:UIViewController
//…你的财产等等。。。
@iAction func signInButtonPressed(发送方:任意对象){
trySigningIn()
}
}
//马克:二等兵
私有扩展mySignenView{
函数trysigning(){
警卫
让email=userEmailTextField.text,
让password=userPasswordTextField.text
否则{
打印(“无法登录,电子邮件或密码为‘nil’”)
返回
}
做{
尝试登录(电子邮件:电子邮件,密码:密码){[unowned self]authResult in
self.userDidSignIn(authResult.user)
}
}抓住{
//显示错误
}
}
func userDidSignIn(uu用户:FIRUser){
//珍妮弗的信条:https://stackoverflow.com/a/51389154/1311272
guard user.iseMail已验证其他{
//TODO显示关于未验证电子邮件用户的消息?
返回
}
性能检查(标识符为“toHome”,发送者为零)
}
}
//马克:错误
私有扩展mySignenView{
枚举错误:Strng,可均衡{
案例无效邮件
案例用户禁用
大小写错误密码
案例userNotFound
案例网络错误
案例未知错误
}
}
//标记:特定于Firebae
私有扩展mySignenView{
职能签名(
电子邮件:String,
密码:String,
onSuccessful:(AuthDataResult)->Void
)投掷{
Auth.Auth().sign(
用email:email,
密码:密码
){(authResult,anyError)在
如果让anyError=anyError{
如果let error=error(anyError:anyError){
抛出错误
}否则{
fatalError(“不支持的错误:\(anyError)”)
}
}
onSuccessful(authResult)
}
}
}
私有扩展mySignenView.Error{
初始化?(任何错误:Swift.Error){
guard let authErrorCode=FIRAuthErrorCode(rawValue:anyError.code)else{
归零
}
self.init(fireBaseAuthErrorCode:authErrorCode)
}
//信条包括:https://stackoverflow.com/a/39936083/1311272
初始化(fireBaseAuthErrorCode:FirautherErrorCode){
开关错误码{
案例.ErrorCodeInvalidEmail:
self=.invalidEmail
案例。ErrorCodeUserDisabled:
self=.userDisabled
case.errorCodeErrorPassword:
self=.errowpassword
案例.ErrorCodeUserNotFound:
self=.userNotFound
案例.ErrorCodeNetworkError:
self=.networkError
违约:
self=.unknown错误
}
}
}
您需要适当地通知用户不同的错误/不正确状态,当然,不仅仅是打印
此代码可能会被移动到
ViewModel
,如果您没有使用MVVM
,我强烈建议:)。谢谢您的帮助,但我得到了一个类型为“AuthDataResult”的错误值,没有成员“isEmailVerified”,我已经更新了我的答案。我建议您查看以了解更多有关Auth的信息。此代码不安全,您不应强制展开选项。
if let email = emailTextfield.text, let password = passwordTextfield.text {
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
if let e = error{
print(e.localizedDescription)
}
else {
//Do whatever you want to do after successful login
}
}
}