如何在firebase中分离两种类型的用户,以分离web和移动app用户的登录



我目前正在研究一个项目,其中停车场业主应该使用网站登录,其他用户应该使用移动应用程序登录。但目前,任何用户都可以登录网站和移动应用程序。

这是我的firebase实时数据库我的实时数据库

如你所见,我在user中定义了type。当用户注册时,根据他/她注册的设备获得类型

和我的web sign in功能是这样的:

signInWithEmailAndPassword(auth, email, password).then((userCredential) => {
const user = userCredential.user;
alert('User Logged in!');
window.location = 'user.html';
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});

如何为具有'type = web'的用户提供登录?

Firebase Authentication只关心凭证:如果您输入的电子邮件/密码与系统中的数据匹配,则可以登录-无论您在哪个平台上。在Firebase Authentication中没有办法改变这一点,所以任何额外的逻辑都必须来自您的应用程序代码。

例如,您可以存储停车场所有者的uid列表,并在登录后检查是否允许他们使用web应用程序。

signInWithEmailAndPassword(auth, email, password).then((userCredential) => {
const user = userCredential.user;
if (user) {
const uid = user.uid;                                  // determine the UID of the user
const ownersRef = firebase.database().ref("parkinglotOwners");
const userSnapshot = await ownersRef.child(uid).get(); // try to load this users data from parkinglotOwners
if (userSnapshot.exists()) {                           // if this data exists
window.location = 'user.html';                       // send them to the web app
} else {
alert("You're not allowed to use this app";          // tell them to go away
}
}
...
}).catch((error) => {

Firebase Auth是关于身份验证的(你是你所说的那个人吗)。

您的需求更多是关于访问控制。Firebase中有一个功能可以帮助解决这个问题。它被称为"海关索赔"。并允许您执行基于声明的访问控制。

请看这个视频:https://www.youtube.com/watch?v=3hj_r_N0qMs

最新更新