我试图弄清楚如何从我的Firestore文档中提取一个字段以用作对角色的检查,我很难理解Firestore的读取方法我发现大多数文档或示例都通过了将值传递给数组,然后传递给HTML本身,但是我想知道如何获取1个字段或获取整个文档并将其传递给一个集合或数组,然后如何调用它的一部分以在应用程序中使用在变量中,您将在下面的代码段中看到我非常糟糕的尝试:
import { User, Roles } from './user';
import { Injectable, NgZone } from '@angular/core';
import { auth } from 'firebase/app';
import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
import { Router } from "@angular/router";
import { userInfo } from 'os';
import * as admin from 'firebase-admin';
@Injectable({
providedIn: 'root'
})
export class AuthService {
userData: any; // Save logged in user data
passwordResetEmail: any;
isAdmin = false;
userinfo: User;
roleinfo: Roles;
adminKey = [
'kUUzdVIy4QhG83PfH7tKSWSzAv72',
];
checkAdmin: any;
items: Array<any>;
constructor(
public afs: AngularFirestore, // Inject Firestore service
public afAuth: AngularFireAuth, // Inject Firebase auth service
public router: Router,
public ngZone: NgZone // NgZone service to remove outside scope warning
) {
/* Saving user data in localstorage when
logged in and setting up null when logged out */
this.afAuth.authState.subscribe(user => {
if (user) {
this.userData = user;
localStorage.setItem('user', JSON.stringify(this.userData));
JSON.parse(localStorage.getItem('user'));
} else {
localStorage.setItem('user', null);
JSON.parse(localStorage.getItem('user'));
}
})
}
createAdmin(adminemail){
return this.afs.collection('adminUsers').add({
email: adminemail
})
.then((result) => {
window.alert('Admin Registered')
}).catch((error) => {
window.alert(error.message);
});
this.gohome();
}
// Sign in with email/password
SignIn(email, password) {
// const admin = this.afAuth.auth.currentUser.admin;
//if (admin === true) {
//this.isAdmin = true;
//this.router.navigate(['dashboard']);
//}
return this.afAuth.auth.signInWithEmailAndPassword(email, password, )
.then((result) => {
this.ngZone.run(() => {
const checker = this.afAuth.auth.currentUser.isAdmin;
if(this.userData === true){
this.isAdmin = true;
this.router.navigate(['dashboard']);
console.log("Firing True")
}else{
this.isAdmin = true;
this.router.navigate(['dashboard']);
console.log("firing false")
}
});
this.SetUserData(result.user);
}).catch((error) => {
window.alert(error.message)
});
}
// Sign up with email/password
SignUp(email, roles) {
//#region generate password
var length = 8,
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
password = "";
for (var i = 0, n = charset.length; i < length; ++i) {
password += charset.charAt(Math.floor(Math.random() * n));
}
//#endregion
return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
.then((result) => {
if(roles === "isAdmin"){
this.roleinfo.role = 'Admin';
}else{
this.roleinfo.role ='User';
}
this.ForgotPassword(email);
this.SetSignUpData(result.user);
}).catch((error) => {
window.alert(error.message);
});
}
// Reset Forggot password
ForgotPassword(passwordResetEmail) {
return this.afAuth.auth.sendPasswordResetEmail(passwordResetEmail)
.then(() => {
window.alert('Email sent, Please check your Inbox');
}).catch((error) => {
window.alert(error)
})
}
// Returns true when user is looged in and email is verified
get isLoggedIn(): boolean {
const user = JSON.parse(localStorage.getItem('user'));
return (user !== null ) ? true : false;
}
// Auth logic to run auth providers
AuthLogin(provider) {
return this.afAuth.auth.signInWithPopup(provider)
.then((result) => {
this.ngZone.run(() => {
this.router.navigate(['dashboard']);
})
this.SetUserData(result.user);
}).catch((error) => {
window.alert(error)
})
}
/* Setting up user data when sign in with username/password,
sign up with username/password and sign in with social auth
provider in Firestore database using AngularFirestore + AngularFirestoreDocument service */
SetUserData(user) {
const userRef: AngularFirestoreDocument<any> = this.afs.doc(`users/${user.uid}`);
const userData: User = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
}
return userRef.set(userData, {
merge: true
})
}
SetSignUpData(user) {
return this.afs.doc(`users/${user.email}`).set({
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
Role: this.roleinfo
})
}
// Sign out
SignOut() {
return this.afAuth.auth.signOut().then(() => {
localStorage.removeItem('user');
this.router.navigate(['sign-in']);
})
}
gohome() {
this.router.navigate(['dashboard']);
}
gosignup(){
this.router.navigate(['register-user']);
}
goAdminsignup(){
this.router.navigate(['adminsignup']);
}
}
当用户登录登录方法时,我需要确定天气是管理员还是用户渴望。以便我可以为这两个创建单独的仪表板
注册工作如下:
管理员输入注册,然后给电子邮件,然后假设要从选择字段中选择注册人是否是管理员(我的席位未载入选项),在那里我正在检查选择器的值,以查看是否选择了管理员或用户,然后在文档中相应地注册角色字段。从那里开始,目标是读取“角色”字段。
或将整个文档传递到我的用户界面中:
export interface User {
uid?: string;
email?: string;
displayName?: string;
photoURL?: string;
}
export interface Roles{
role: string;
}