简体   繁体   English

Firebase检查用户/用户是否存在的身份验证状态

[英]Firebase check auth status of user/ if user exists

So I built a simple app using Firebase Authentication (with just email and password) and it was working great, but in order to TestFlight my sign up/login page I needed to delete the accounts of everyone who had signed up with the app previously, only to find that deleting the users on the console doesn't actually deauth them in the app. 所以我使用Firebase身份验证(只有电子邮件和密码)构建了一个简单的应用程序,它运行良好,但为了TestFlight我的注册/登录页面,我需要删除之前注册过该应用程序的所有人的帐户,只是发现删除控制台上的用户实际上并没有在应用程序中取消它们。 I would imagine there would be a way to check a user's authentication status in the Firebase console (if they exist or not at least) but I can't find that functionality to save my life. 我想有一种方法可以在Firebase控制台中检查用户的身份验证状态(如果它们存在或至少存在),但我找不到保存生命的功能。 Any help is welcome and appreciated! 欢迎任何帮助和赞赏!

I ran into this same issue and found a workaround that I've been using ever since. 我遇到了同样的问题,并找到了我一直在使用的解决方法。 Instead, I just query my database in /users (a category I created for users) and check if my current ID exists. 相反,我只是在/ users(我为用户创建的类别)中查询我的数据库,并检查我当前的ID是否存在。 If it does not, I know the account has been deleted. 如果没有,我知道该帐户已被删除。 This means you need to create an entry with your userID in /users on sign up and delete this entry when you delete the account. 这意味着您需要在注册时在/ users中创建包含userID的条目,并在删除帐户时删除此条目。 To see if currently authenticated user is deleted then, do something like this: 要查看当前已验证的用户是否已删除,请执行以下操作:

NSString *currentID=[[FIRAuth auth].currentUser uid];
[[[[[FIRDatabase database]reference]child:@"users"]child:currentID]observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
    if (snapshot!=[NSNull Null]) {
        //User still exists
    } else {
        //Account no longer exists (deleted)
    }
}];

I spent all day since posting this trying to do the whitelist solution and couldn't get it to work. 我花了一整天的时间发布这个试图做白名单解决方案,但无法让它工作。 However, a friend of mine sent me a solution similar to this and it works like a charm. 然而,我的一个朋友给我发了一个类似于此的解决方案,它就像一个魅力。

func checkUserAgainstDatabase(completion: (success: Bool, error: NSError?) -> Void) {
    guard let currentUser = FIRAuth.auth()?.currentUser else { return }
    currentUser.getTokenForcingRefresh(true) { (idToken, error) in
        if let error = error {
            completion(success: false, error: error)
            print(error.localizedDescription)
        } else {
            completion(success: true, error: nil)
        }
    }
}

The code below works great on android to confirm if the Firebase Auth user still exists (has not been deleted or disabled) and has valid credentials. 下面的代码在Android上运行良好,以确认Firebase Auth用户是否仍然存在(尚未删除或禁用)并具有有效凭据。

Deleting the Auth user from the firebase console does not revoke auth tokens on devices the user is currently logged in as the token is cached locally. 从firebase控制台删除Auth用户不会撤消用户当前登录的设备上的auth令牌,因为令牌在本地缓存。 Using reload() forces a check with the firebase auth server. 使用reload()强制检查firebase auth服务器。

mFirebaseUser.reload().addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        //User still exists and credentials are valid

                    }else {
                        //User has been disabled, deleted or login credentials are no longer valid, 
                        //so send them to Login screen
                    }
                }
            });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM