如何在Firebase中创建管理员用户等?

时间:2016-11-02 12:41:30

标签: javascript firebase vue.js 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)可能有任何如何让用户使用不同的例子...... 谢谢!

0 个答案:

没有答案