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

时间:2016-11-03 08:34:24

标签: 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申请)

谢谢!

1 个答案:

答案 0 :(得分:0)

我在这里参加聚会有点晚,但也可以为任何期待的人解答。 使用Firebase向用户分配角色的当前方法是使用自定义声明。为此,您需要铸造自己的JWT,然后添加自定义声明。您可以使用Firebase Functions来执行此操作,也可以在自己的服务器上执行此操作。

您可以查看文档here