[英]how do i restrict pages in ionic and firebase-authentication
As far as I can tell what I'm after is pretty basic, but I can't seem to find a clear solution to it, at least in the way I've built it up so far.据我所知,我所追求的是非常基本的,但我似乎无法找到一个明确的解决方案,至少到目前为止我已经建立了它。 I'm now completely stumped.
我现在完全被难住了。
I've got the firebase email/password login working perfectly and redirecting to a registrants page.我的 firebase 电子邮件/密码登录正常工作并重定向到注册页面。 However I obviously want some safeguard in the registrants page to say 'If I'm not logged in, go back to the login page'.
但是,我显然希望在注册页面中提供一些保护措施,说“如果我没有登录,go 返回登录页面”。
In my authentication-service.ts
I've set up a property of the class authService
called isLoggedIn
that just returns a true/false:在我的
authentication-service.ts
中,我设置了一个名为isLoggedIn
的 class authService
的属性,它只返回 true/false:
get isLoggedIn(): boolean {
const user = JSON.parse(localStorage.getItem('user'));
console.log('user: ' + user);
return (user !== null ) ? true : false;
}
So I presumed (wrongly) that I could put the following in the ngOnInit()
in the registrants page after the user is logged in, just so it'll fire up a check if the user is logged in once the page is visited, I can enter the redirectByUrl
once I know it's working:所以我假设(错误地)我可以在用户登录后将以下内容放在注册页面的
ngOnInit()
中,这样它就会在访问页面后检查用户是否登录,我一旦我知道它正在工作,就可以输入redirectByUrl
:
console.log('loggedin: ' + this.authService.isLoggedIn);
But I've learned that ngOnInit
is only ever called once.但我了解到
ngOnInit
只被调用过一次。 So you could go into the page, then logout, then go back and it won't check if the user is logged in.所以你可以 go 进入页面,然后注销,然后 go 回来,它不会检查用户是否登录。
It works when i refresh the page, but not when it visits it within links.当我刷新页面时它起作用,但当它在链接中访问它时不起作用。 Is there a certain lifecycle hook that will check every time someone clicks on the link to visit the page or is it done a much simpler way?
是否有特定的生命周期挂钩会在每次有人单击链接访问页面时进行检查,还是以更简单的方式完成?
I've realised I can put it in a ngDoCheck()
but that seems a little excessive doesn't it?我意识到我可以把它放在
ngDoCheck()
中,但这似乎有点过分,不是吗?
have you tried using ionViewWillEnter
?您尝试过使用
ionViewWillEnter
吗?
https://ionicframework.com/docs/angular/lifecycle https://ionicframework.com/docs/angular/lifecycle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.