且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase 身份验证登录必须允许单台设备登录

更新时间:2023-12-03 19:47:16

我在上面通过 firebase 身份验证面临的问题上进行了大量搜索,经过大量研究后,我最终得到了以下解决方案,该解决方案符合我的要求.

I search a lot on above issue which I was facing through firebase authentication and after lots of research I ended up with below solution which was working as per my requirements.

首先,firebase 没有在他们的库中提供此功能,因此我们需要在此处应用我们的自定义逻辑以在我们的应用中实现 1 个会话用户登录.

First of all firebase not providing this in their library so we need to apply our custom logic here to achieve this 1 session user login in our app.

第 1 步:您需要在数据库的根目录中添加新的子项登录".

Step 1: You need to add new child "SignIn" at your root of Database.

第 2 步:Auth.auth().signIn() 在该块中返回成功时,我们需要检查以下标志,即用户已在任何其他设备上登录?为此,我创建了一种方法,如下所述.

Step 2: While Auth.auth().signIn() return success in that block we need to check below Flag that is User already signIn in any other device ? for that I have create one method as mention below.

func alreadySignedIn() {
        // [START single_value_read]
        let userID = Auth.auth().currentUser?.uid
        ref.child("SignIn").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get user value

            if let dict = snapshot.value as? [String: Any] {
                if let signedIn = dict["signIn"] as? Bool {
                    if signedIn {
                        self.signOut()
                    }
                    else {
                        // change the screen like normal
                        print("First Session of user")
                        self.writeNewUserSigin(withUserID: userID!)
                    }
                }else{
                    self.writeNewUserSigin(withUserID: userID!)
                }
            }else{
                print(snapshot)
                self.writeNewUserSigin(withUserID: userID!)
            }
        }) { (error) in
            print(error.localizedDescription)
        }
        // [END single_value_read]
    }

通过这种方法,我们正在检查当前用户 uId 在我们的 SignIn Child 中是否具有 True 值,如果我们的数据库中存在 Boll 值为 True 的数据,我们需要处理它并显示一些来自 firebase 的警报和 signOut.

By this method we are checking that current user uId have in our SignIn Child with True value if data is there in our database with Boll value True we need to handle that and show some alert and signOut from firebase.

注意:由于我们允许用户登录,因此我们正在检查用户已经在任何其他设备上登录,所以如果它返回 True 我们需要从 Firebase 中 SignOut().

Note : As we allowed user to sign-in and than we are checking that user already signin in any other device so if its returning True we need to SignOut() from firebase.

现在是用户手动退出应用程序的最后一步

第 3 步:当用户点击应用程序中的退出按钮时,我们需要使用 False 值更新我们的孩子,以便之后用户可以在任何其他设备上登录.为此,我们可以使用以下方法.

Step 3: While user click on SignOut button in app we need to update our Child with False value in it so after onwards user can able to SignIn in any other device. For that we can use below method.

func updateUserSigIn(withUserID userID: String) {
        //Update SignIn Child with flase value on current UID
        // [START write_fan_out]

        let post = ["signIn": false]
        let childUpdates = ["/SignIn/(userID)": post]
        let ref = Database.database().reference()
        ref.updateChildValues(childUpdates) { (error, refDatabase) in
            if (error != nil) {
                print("error (String(describing: error))")
            }else {
                print("New user Saved successfully")
                self.signOut()
            }
        }
        // [END write_fan_out]
    }

现在只允许一个应用用户会话.

Thats it now only one app user session will allow.

希望这会对其他人有所帮助.

Hope this will helps others.

感谢这个线程,因为我从中得到了一些提示回答.

Thanks for this thread as I got some hints from this answer.