javascript - 如何在 Firebase 角色中添加用户?

标签 javascript firebase firebase-authentication firebase-security

我不太明白不同权限的用户如何操作,以下是规则:

{"rules": 
    {"authentication": 
        {"users": 
            {"$uid": {
                ".read": "auth.uid == $uid || root.child('authentication').child('users').child('auth.uid').child('isAdmin').val() == true",
                ".write": "newData.parent().parent().parent().child('authentication').child('users').child('auth.uid').child('isAdmin').val() == true",
                ".indexOn": [
                    "email"
                ]
                }
            }
        }
    }
}

用户:link screen user

实时数据库:

{
 "users" : {
 "-KVVe4Ncd5Qnm5r37zVp" : {
   "email" : "admin@gmail.com",
   "isAdmin" : true,
   "name" : "admin"
 },
  "-KVVeADyh07mBXBFImtq" : {
   "email" : "djvang92@gmail.com",
   "isAdmin" : true,
   "name" : "Ivan"
  }
 }
}

Firebase 脚本:

// Initialize Firebase
const config = {
  apiKey: "AIzaSyAfeEpMopsPWnowiv1uEWYINgk6V_ohvG4",
  authDomain: "spalah-1358.firebaseapp.com",
  databaseURL: "https://spalah-1358.firebaseio.com",
  storageBucket: "spalah-1358.appspot.com",
  messagingSenderId: "300000265085"
}

firebase.initializeApp(config)


export const db = firebase.database();

export const auth = firebase.auth()



console.log(auth);

// var provider = new firebase.auth.GoogleAuthProvider();
// var provider = new firebase.auth.FacebookAuthProvider();
// var provider = new firebase.auth.TwitterAuthProvider();
// var provider = new firebase.auth.GithubAuthProvider();




export default {

  // User object will let us check authentication status
  user: {
    authenticated: false,
    data: null,
    message: ''
  },
  login(context, creds, redirect) {
    console.log('Login...');
    let self = this;
    let email = creds.email;
    let password = creds.password;
    let promise = auth.signInWithEmailAndPassword(email, password);

    console.log(email, password);

    // Redirect to a specified route
    if(redirect) {
       // context.$router.go(redirect) 
       // console.log(context.$router);       
    }

    promise
    .then(user => {
       self.user.authenticated = true
       self.user.message = false
    })
    .catch(e => {
      self.user.message = e.message
      console.log(e);
    })

  },
  signup(context, creds, redirect) {
    console.log('Sign Up...');
    let email = creds.email
    let password = creds.password
    let promise = auth.createUserWithEmailAndPassword(email, password);

    promise
      .then(user => {
        console.log(user);
        self.user.message = false
      })
      .catch(e => {
        self.user.message = e.message
        console.log(e);
      })
  },
  logout() {
    let self = this
    auth.signOut().then(function() {
      // Sign-out successful.
      console.log('Log-out successful');
      self.user.authenticated = false
    }, function(error) {
      // An error happened.
      console.log('Log-out error');
    });
  },
  provider(context, creds, redirect) {
    let provider = new firebase.auth.GoogleAuthProvider()

    auth.signInWithPopup(provider).then(function(result) {
      // Accounts successfully linked.
      var credential = result.credential;
      var user = result.user;
      // ...
      console.log(credential);
      console.log(user.photoURL);
    }).catch(function(error) {
      // Handle Errors here.
      // ...
      console.log(error);
    });

  },
  checkAuth() {
    let self = this
    auth.onAuthStateChanged(function(user) {
      if (user) {
        // User is signed in.
        console.log('Connect:', user);
        self.user.authenticated = true
        self.user.data = user
      } else {
        // No user is signed in.
        console.log('No connect:');
        self.user.authenticated = false
        self.user.data = null
      }
    });
  }
}

我不明白如何在 Javascript 中测试它...... (我向vue.js申请)

谢谢!

最佳答案

我来参加聚会有点晚了,但也可以为任何正在寻找的人回答这个问题。 当前使用 Firebase 向用户分配 Angular 色的方法是使用自定义声明。为此,您需要创建自己的 JWT 并在此时添加自定义声明。您可以使用 Firebase Functions 来执行此操作,也可以在您自己的服务器上执行此操作。

可以查看文档here .

关于javascript - 如何在 Firebase 角色中添加用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396897/

相关文章:

android - 如何知道哪个用户正在尝试登录?

android - 验证用户名和电子邮件崩溃,无法插入到 firebase 数据库

javascript - React-router-dom 链接页面无法正常工作

Firebase函数获取用户电话号码

ios - 在 swift 中以编程方式创建的 firebase 动态链接不起作用,没有域

lazy-loading - Firebase 延迟加载

firebase - 使用 signInWithEmailAndPassword 时在 auth/wrong-password 上获取 Firebase User.ProviderID

javascript - 带有 HTML 5 的混合 iOS 应用程序,如何为远程服务器提供凭据

javascript - 如何在 Javascript 中隐藏闪烁 20 秒后的 div 或图像?

javascript - 如何在 JS "onclick"中使用 "setAttribute"运行函数?