多角色身份验证Firebase Web [英] Multiple role authentication Firebase Web

查看:81
本文介绍了多角色身份验证Firebase Web的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户的角色对他们进行身份验证.例如,我有3种类型的角色:普通用户,高级用户和管理员.每种类型的用户在登录时都会重定向到特定页面.

I want to authenticate users based on their role. For example, I have 3 types of role: a normal user, a premium user and an Admin. Each type of users gets redirected to a specific page when he logs in.

根据官方文档,我仅设法对一种类型的用户进行了简单的身份验证. 我想知道如何注册具有不同角色的用户,然后将他们分别登录到特定页面.

I only managed to make a simple authentication for only one type of users based on the official documentation. I want to know how to sign up users with different roles then log them each to a specific page.

推荐答案

基于预先存在的管理员"和高级"用户列表来控制每个用户看到的体验的最简单方法可能是自定义声明.

The easiest way to control what experience each user sees based off a pre-existing list of "Admin" and "Premium" users would probably be with custom claims.

如果您仅使用标准Firebase登录名,则可以使用管理SDK .

If you are just using the standard Firebase login, the way to do this would be with the Admin SDK.

如果您要在注册时验证用户,则需要使用

If you want to validate users as they sign up, you'll want to use custom login system to control signups. You can also set the custom claims from there.

用户登录后,便可以将其重定向到正确的页面.

Once the user is logged in, you can then redirect them to the correct page.

firebase.auth().signInWithEmailAndPassword(email, password)
    .catch(function(error) {
  // Handle Errors here.
}).then(function(){
  firebase.auth().currentUser.getIdToken()
  .then((idToken) => {
     // Parse the ID token.
     const payload = JSON.parse(b64DecodeUnicode(idToken.split('.')[1]));
     // Confirm the user is an Admin.
     if (!!payload['admin']) {
       redirectAdminUI();
     }
  })
  .catch((error) => {
    console.log(error);
});

这篇关于多角色身份验证Firebase Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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