且构网

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

多角色身份验证Firebase Web

更新时间:2023-12-06 08:16:46

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

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);
});