简体   繁体   English

我如何限制 ionic 和 firebase-authentication 中的页面

[英]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.

相关问题 如何在 Firebase 身份验证中限制 email 域 - How to restrict email domains in Firebase Authentication Firebase 身份验证如何与离子和电容器一起使用? - How does firebase authentication work with ionic and capacitor? 如何使用 Firebase 身份验证制作受保护的网页/面板? - How do I make a protected webpage/panel with Firebase authentication? 如果 firebase 没有身份验证,如何限制 API 密钥? - How to restrict API key if firebase don't have authentication? 如何使用 Firebase 在 Flutter 中进行电话身份验证? - How to do Phone Authentication in Flutter using Firebase? 如何通过 npm 在 Ionic2 中使用 firebase v3? - How do I use firebase v3 in Ionic2 via npm? 如何获取我刚刚在 Node.js 上使用 firebase 身份验证创建的用户的令牌 ID? - How do I get the token ID of a user that I just created using firebase authentication on Node js? Firebase 认证棒电容 Ionic on iOS - Firebase Authentication Sticks Capacitor Ionic on iOS 如何使用 flutter 检查用户是否已存在于 firebase 身份验证中? - How do I check if a user is already exist in firebase Authentication using flutter? Firebase9 - 如何限制非注册用户登录? - Firebase9 - How can i restrict non-registered users from signing in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM