[英]How to add a user in the role Firebase?
I can not quite understand how do users with different rights, here are the rules: 我不太明白具有不同权限的用户是如何规则的:
{"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"
]
}
}
}
}
}
user: link screen user user: 链接屏幕用户
Realtime Database: 实时数据库:
{
"users" : {
"-KVVe4Ncd5Qnm5r37zVp" : {
"email" : "admin@gmail.com",
"isAdmin" : true,
"name" : "admin"
},
"-KVVeADyh07mBXBFImtq" : {
"email" : "djvang92@gmail.com",
"isAdmin" : true,
"name" : "Ivan"
}
}
}
Firebase script: 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
}
});
}
}
I can not understand how to test it in Javascript... (I make application to vue.js) 我无法理解如何在Javascript中测试它...(我向vue.js申请)
Thank you! 谢谢!
I'm a bit late to the party here but may as well answer this for anyone looking. 我在这里参加派对有点晚了但是对于任何想看的人都可以回答这个问题。 The current method for assigning roles to users with Firebase is to use custom claims.
使用Firebase为用户分配角色的当前方法是使用自定义声明。 To do this you mint your own JWTs and add in the custom claims at this point.
为此,您可以制作自己的JWT并在此处添加自定义声明。 You can use Firebase Functions to do this or you could do this on your own server.
您可以使用Firebase功能执行此操作,也可以在自己的服务器上执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.