【发布时间】:2022-01-07 23:21:22
【问题描述】:
我正在尝试使用以下代码在 angularfire 中通过持久性来实现身份验证:
constructor(private auth: Auth, private router: Router) {
if (auth.currentUser) this.router.navigate(this.redirect);
}
async loginWithGoogle() {
const provider = new GoogleAuthProvider();
try {
await setPersistence(this.auth, browserLocalPersistence);
await signInWithPopup(this.auth, provider);
this.message = 'Sign in successful';
await this.router.navigate(this.redirect);
} catch (error: any) {
console.error(error);
if (error.code) {
this.message = `${error.code}: ${error.message}`;
} else {
this.message = 'There was a problem signing in. Please try again.';
}
}
}
但是,无论setPersistence 方法的位置如何,我总是会收到此错误:
Class constructor BrowserLocalPersistence cannot be invoked without 'new'
我按照文档 (https://firebase.google.com/docs/auth/web/auth-state-persistence) 到了 T;我做错了什么?
我正在使用 Angular 13、Angularfire 7 和 Firebase 9。
【问题讨论】:
-
如果您知道您有特定的用例需要,我强烈建议您只致电
setPersistence。在大多数浏览器上,Firebase 已经在重新加载之间保持身份验证状态,而无需调用setPersistence。 -
我没有看到...我想我什至不需要设置持久性;谢谢!
标签: angular firebase firebase-authentication angularfire2 rxfire