gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:22:56 25 4
gpt4 key购买 nike

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

{"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 Angular 色中添加用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40396897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com