我正在Angular 10项目中工作,也正在使用Firebase托管和Cloud Firestore(用于DB)。我也在我的项目中使用AngularFire。
我的项目已经能够从Firestore集合中获取文档并显示它们(也可以编辑,删除和创建它们)。我还设置了身份验证,使用AngularFireAuth登录和注销。我也有路由防护,仅允许用户登录后访问信息。
我发现Firestore也有规则,您应该设置规则以保护您的收藏。目前,我基本上没有规则(测试模式),但是我想添加一个基本的“只有用户可以访问任何内容”规则,但是遇到了问题。
我认为这是问题,目前,登录我的应用程序后,会将用户存储在本地存储中。我认为我需要以其他方式存储它,以便从先前给出的凭据重新登录,而不是仅检查是否存在本地存储。只有ERROR FirebaseError: Missing or insufficient permissions
当我的保安检查本地存储以确保登录时,我才会收到错误消息,如果我先登录,则不会收到该错误消息。
因此,我应该如何保存用户数据,这样就不必每次刷新时都登录,而是可以验证对Firebase的身份验证?我知道我可以将电子邮件/密码存储到本地存储中,然后检查以重新登录,但这对我来说并不安全。
我认为以上是问题,但不是100%肯定。
这是我的Firestore规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
allow read, write: if request.auth != null
match /{document=**} {
allow read, write: if request.auth != null //should only allow users?
}
}
}
这是我的身份验证服务(我在其中处理登录/注销,并检查本地存储中是否有用户。
export class AuthService {
constructor(private aFAuth: AngularFireAuth, public router: Router) {
//I honestly don't know if I need this
this.aFAuth.authState.subscribe((user) => {
if (user) {
localStorage.setItem('my-test-app-currentUser', JSON.stringify(user));
} else {
localStorage.setItem('my-test-app-currentUser', null);
}
});
}
async signIn(email: string, password: string) {
this.aFAuth
.signInWithEmailAndPassword(email, password).then((result) => {
localStorage.setItem('my-test-app-currentUser', JSON.stringify(result.user));
this.router.navigate(['']);
}).catch((error) => {
window.alert(error.message);
});
}
//this is the func that needs to change, if I have storage, I need to be able to sign-in with it again
isSignedIn(): boolean {
if (!localStorage.getItem('my-test-app-currentUser')) {
return false;
}
return true;
}
signOut() {
return this.aFAuth.signOut().then(() => {
localStorage.removeItem('my-test-app-currentUser');
window.alert('You have been signed-out');
});
}
}
这是我的警卫:
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
// return true;
if (this.auth.isSignedIn()) {
return true;
} else {
this.router.navigate(["sign-in"]);
return false;
}
}
任何帮助,将不胜感激。
Firebase已经将用户凭据存储在本地存储中,并在您重新加载页面时自动将其还原。
还原它们确实需要对服务器进行检查,因此它是异步发生的。因此,任何依赖于用户身份验证状态的代码都应位于 this.aFAuth.authState.subscribe
处理程序内部,以便只要身份验证状态发生更改,代码便会运行。
因此signInWithEmailAndPassword
,导航应该在auth侦听器中进行,而不是在调用完成时处理导航(仅当您主动登录用户时才发生导航),导航既可以在活动登录时又可以在还原时运行。
所以像:
export class AuthService {
constructor(private aFAuth: AngularFireAuth, public router: Router) {
//I honestly don't know if I need this
this.aFAuth.authState.subscribe((user) => {
if (user) {
localStorage.setItem('my-test-app-currentUser', JSON.stringify(user));
this.router.navigate(['']);
} else {
localStorage.setItem('my-test-app-currentUser', null);
}
});
}
async signIn(email: string, password: string) {
this.aFAuth
.signInWithEmailAndPassword(email, password).then((result) => {
window.alert(error.message);
});
}
...
在您中,canActivate
您可能需要使用AngularFireAuthGuard
。这样可以确保未经身份验证的用户不允许导航到受保护的路由。我认为这可能会取代您对本地存储的全部需求。
另请参阅“ Firebase身份验证入门”和“使用AngularFire防护路由用户”中的AngularFire文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句