我正在尝试使用以下代码在 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
.
关于没有 'new' 无法调用 AngularFire 身份验证持久性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69292778/