Firebase:防止在多台设备上使用同一个帐户 [英] Firebase : Prevent same account on multiple devices

查看:25
本文介绍了Firebase:防止在多台设备上使用同一个帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Angular 应用程序,我使用 Firebase 来验证我的用户.我想知道如何防止我的用户将他们的帐户提供给其他人.另外我想防止人们使用同一个帐户同时从不同的设备登录.我找到了一些非常好的教程来构建在线状态系统,但是这些系统不会阻止许多不同的人在多个设备上使用同一个帐户.我已经能够检查用户是否正在尝试使用已经在使用的帐户(在线),但我无法注销其中一个用户(使用已经在线的帐户..).我试图在 signInwithemailAndPassword() 方法中调用 auth.signout() 但它不起作用,我没有成功注销用户.感谢您的帮助.我需要的是一个片段,因为从理论上讲,一切都很简单.

I'm working on an angular app and I use Firebase to authenticate my users. I would like to know how I could prevent my users to give their account to other people. Also I would like to prevent people to use the same account to login from different devices at the same time. I found some very good tutorials to build a presence system, but these system doesn't prevent the same account to be used by many different people on several devices. I have been able to check if a user is trying tu use an account that is already in use (online) but I can't manage to log out one of those users (using an alreaydy online account..). I tried to call auth.signout() inside the signInwithemailAndPassword() method but it doesn't work, I don't succeed in logout the users. Thank you for your help. What I would need is a snippet because theorically, everything is very simple.

推荐答案

由于你没有说明你使用的是什么语言,所以我打算使用 Swift,但我在这里列出的背后的原则是一样的对于任何语言.

Since you didn't state what language you're using I'm just going to use Swift, but the principles behind what I laid out here are the same for any language.

看看这个问题.Firebase 似乎不直接支持您要查找的内容.但是,您可以执行以下操作:

Take a look at this question. It appears that Firebase does not directly support what you are looking for. You can however, do something like this:

在您的数据库中创建一个树来存储用户登录的布尔值.

Create a tree in your database that stores a boolean value for user signins.

SignedIn: {
    uid1: {
        "signedIn": true
    }
    uid2: {
        "signedIn": false
    }
    .....
}

我假设在身份验证后您更改屏幕的某些位置.您现在需要在执行此操作之前执行附加查询.如果用户已经登录,您可以显示警报,否则您可以像往常一样继续.

I'm assuming some where after authentication you change the screen. You'll now want to perform an additional query before doing that. If the user is already signed in you can display an alert, otherwise you can just continue as you always did.

func alreadySignedIn() {
     if let uid = Auth.auth().currentUser?.uid {
        Database.database().reference().child("SignedIn").child(uid).observeSingleEvent(of: .value, with: { snap in
            if let dict = snap.value as? [String: Any] {
                if let signedIn = dict["signedIn"] as? Bool {
                    if signedIn {
                        // display an alert telling the user only one device can use
                        // there account at a time
                    }
                    else {
                        // change the screen like normal
                    }
                }
            }
        })
     }
}

当然,这只是防止帐户同时被共享".如果您只允许基于设备 ID 登录,则可以制定更严格的准则.例如,您可以获取设备 ID 并且仅允许在该设备上登录.您必须允许用户在获得新设备时进行更新,但如果您真的想锁定您的帐户,这可能是一个更好的选择.

Of course this just prevents the account from being "shared" at the same time. You can make a stricter guideline if you only allow sign in based on a device id. For example you could get the device id and only allow sign in on that device. You'd have to allow users to update this when they get a new device, but if you really want to lock your accounts down this might be a better option.

这篇关于Firebase:防止在多台设备上使用同一个帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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