Swift 3 Firebase:当前用户登录成功,事件已经在Firebase控制台中删除此用户 [英] Swift 3 Firebase: current user login success, event already delete this user in the Firebase Console

查看:227
本文介绍了Swift 3 Firebase:当前用户登录成功,事件已经在Firebase控制台中删除此用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在登录表单中通过代码成功创建Firebase用户之后。我在Firebase控制台中删除了这个新用户,退出了该应用。当再次打开应用程序时,它仍然通知管理员已经删除的用户登录成功。为什么会这样,以及如何在登录表单中知道用户已经删除了?



请参阅我下面的登录表单代码:

 覆盖func viewDidLoad(){
super.viewDidLoad()

//获取当前用户是通过设置侦听器Auth对象:
FIRAuth.auth()!. addStateDidChangeListener(){auth,user
if user!= nil {
//用户登录
print start login success:+(user?.email)!)
//self.performSegue(withIdentifier:loginToList,sender:nil)
} else {
//没有用户签名in。
print(没有用户登录)
}

}

//通过使用获取当前登录的用户currentUser属性。如果用户没有登录,currentUser是零:
如果让curUser = FIRAuth.auth()?currentUser {
//用户登录
print(start current user:+ curUser.email!)
} else {
//目前没有用户登录
print(目前没有用户登录)
来自

解决方案

因为用户仍然记录。
第一步是注销,你可以在appDelegate里面的applicationDidEnterBackground函数中做到这一点。只需调用这个:

试试! FIRAuth.auth()!。signOut()

  func applicationDidEnterBackground(_ application:UIApplication){ 
//使用此方法释放共享资源,保存用户数据,使计时器无效,并存储足够的应用程序状态信息,以便将应用程序恢复到当前状态,以防稍后终止。
//如果您的应用程序支持后台执行,则调用此方法而不是applicationWillTerminate:当用户退出时。

试试! FIRAuth.auth()!。signOut()
}

第二步是一个新的登录(如果用户允许保存电子邮件和密码在... userDefault ....例如)并得到有关用户在applicationDidBecomeActive函数内登录的新信息。

  func applicationDidBecomeActive(_ application:UIApplication){
//在应用程序处于非活动状态时,重新启动暂停(或尚未启动)的任务。如果应用程序以前位于后台,则可以选择刷新用户界面。

//检查用户数据是否存在

让User = UserDefaults.standard

let let myEmail = User.object(forKey:userName )为? String else {

print(No User)

return
}

//检查用户是否需要自动访问

guard User.object(forKey:automaticLogIn)as! Bool else {

print(Automatic LogIn disable)
return
}

//现在您可以执行自动登录

让password = User.object(forKey:password)as!

FIRAuth.auth()?。signIn(withEmail:myEmail,password:password,completion:{(user,error)in
$ b $ if error == nil {

print(logIn succesfull)

} else {

let typeError = FIRAuthErrorCode(rawValue:error!._ code)!
开关类型错误{
case .errorCodeUserNotFound:
print(user not found)
case .errorCodeWrongPassword:
print(错误的密码)
默认值:
$ break

}
}
})


}
FIRAuth.auth()!. addStateDidChangeListener()
,例如你想要的地方in viewDidLoad

  override func viewDidLoad(){
super.viewDidLoad()

/ /保存在userDefault里面的用户数据,你可以做你想要的地方....像一个表格里面alertView

让User = UserDefaults.standard
User.set(userEmail ...,forKey:userName)
User.set(UserPassword,forKey :password)
User.set(true,forKey:automaticLogIn)

FIRAuth.auth()!. addStateDidChangeListener(){auth,user in $ b $ if user != nil {
//用户已登录
print(start login success:+(user?.email)! )
//self.performSegue(withIdentifier:loginToList,sender:nil)
} else {
//没有用户登录
print(没有用户登录)
}

}

}


After create the Firebase user successful by code in login form. I deleted this new user in Firebase console, quit the app. When open the app again, it still inform login success with the user already deleted by admin. Why is this, and how to know in the login form that user already deleted?

Refer to my below code of login form:

override func viewDidLoad() {
    super.viewDidLoad()

    // get the current user is by setting a listener on the Auth object:
    FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
        if user != nil {
            // User is signed in.
            print("start login success: " + (user?.email)! )
            //self.performSegue(withIdentifier: loginToList, sender: nil)
        } else {
            // No user is signed in.
            print("No user is signed in.")
        }

    }

    //get the currently signed-in user by using the currentUser property. If a user isn't signed in, currentUser is nil:
    if let curUser = FIRAuth.auth()?.currentUser {
        // User is signed in.
        print("start current user: " + curUser.email! )
    } else {
        // No current user is signed in.
        print("Currently, no user is signed in.")
    }
}
}

sample code from https://firebase.google.com/docs/auth/ios/manage-users

解决方案

Because the user is still logged. The first step is logout, you can do it in appDelegate inside applicationDidEnterBackground function. Just call this:
try! FIRAuth.auth()!.signOut()

 func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

     try! FIRAuth.auth()!.signOut()
}

The second step is to do a new login ( if the user allowed to save the email and password in... userDefault.... for example) and get the new information about the login of the user inside applicationDidBecomeActive function.

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

  // Check if the User data exist

    let User = UserDefaults.standard

    guard let myEmail = User.object(forKey: "userName") as? String  else {

        print("No User")

        return
    }

    //Check if the user wants the automatic access

    guard User.object(forKey: "automaticLogIn") as! Bool else {

        print("Automatic LogIn disable")
        return
    }

    // Now you can to do the automatic login

    let password = User.object(forKey: "password") as! String

    FIRAuth.auth()?.signIn(withEmail: myEmail, password: password, completion: { (user, error) in

        if error == nil{

            print("logIn succesfull")

        }else{

            let typeError = FIRAuthErrorCode(rawValue: error!._code)!
            switch typeError {
            case .errorCodeUserNotFound:
                print("user not found")
            case .errorCodeWrongPassword:
                print("wrong password")
            default:
                break

            }
        }
    })


}

And now you can call FIRAuth.auth()!.addStateDidChangeListener(), where you want, for example in viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

 //  Save the user data inside userDefault, you can do it where you want.... like a form inside an alertView

    let User = UserDefaults.standard
    User.set("userEmail...", forKey: "userName")
    User.set("UserPassword", forKey: "password")
    User.set(true, forKey: "automaticLogIn")

FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
        if user != nil {
            // User is signed in.
            print("start login success: " + (user?.email)! )
            //self.performSegue(withIdentifier: loginToList, sender: nil)
        } else {
            // No user is signed in.
            print("No user is signed in.")
        }

    }

}

这篇关于Swift 3 Firebase:当前用户登录成功,事件已经在Firebase控制台中删除此用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆