Angular + 令牌的 Firebase 持久性
Angular + Firebase Persistance of token
我想让用户登录持久化(当他关闭页面时,他应该不需要再次登录。根据文档,我是这样做的:
signinUser(email: string, password: string) {
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(function() {
return firebase.auth().signInWithEmailAndPassword(email, password)
.then(
response => {
console.log(response);
this.router.navigate(['/']);
}
);
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
}
但我没有导航。奇怪的是,我注入了路由器:
constructor(private router: Router) {}
它告诉我:
[ts] Property 'router' is declared but its value is never read.
而且我明明在用。不知何故,我无法进入 signInWithEmailAndPassword 的承诺。
有什么帮助吗?谢谢
在第一个 then 调用(...Persistence.LOCAL 之后的调用)中,您有一个函数声明。不像箭头函数,它不会保留当前上下文所以当你使用 this.router
, this 将是上面定义的上下文功能。
所以警告是正确的,您没有使用在 class 上定义的路由器,您在函数上下文中使用了未定义的路由器 属性。你可能看不到任何错误,因为里面没有捕获。
解决这个问题的最快方法是在所有地方使用箭头函数,例如:
signinUser(email: string, password: string) {
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(() => firebase.auth().signInWithEmailAndPassword(email, password)
.then(
response => {
...
您可以查找有关 this here 的更多信息。
我想让用户登录持久化(当他关闭页面时,他应该不需要再次登录。根据文档,我是这样做的:
signinUser(email: string, password: string) {
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(function() {
return firebase.auth().signInWithEmailAndPassword(email, password)
.then(
response => {
console.log(response);
this.router.navigate(['/']);
}
);
})
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
});
}
但我没有导航。奇怪的是,我注入了路由器:
constructor(private router: Router) {}
它告诉我:
[ts] Property 'router' is declared but its value is never read.
而且我明明在用。不知何故,我无法进入 signInWithEmailAndPassword 的承诺。
有什么帮助吗?谢谢
在第一个 then 调用(...Persistence.LOCAL 之后的调用)中,您有一个函数声明。不像箭头函数,它不会保留当前上下文所以当你使用 this.router
, this 将是上面定义的上下文功能。
所以警告是正确的,您没有使用在 class 上定义的路由器,您在函数上下文中使用了未定义的路由器 属性。你可能看不到任何错误,因为里面没有捕获。
解决这个问题的最快方法是在所有地方使用箭头函数,例如:
signinUser(email: string, password: string) {
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(() => firebase.auth().signInWithEmailAndPassword(email, password)
.then(
response => {
...
您可以查找有关 this here 的更多信息。