[英]I am trying to see if the user is logged in and if not push them to the login page
我正在尝试使用ionViewWillEnter检查用户是否已登录。如果返回false,则将其定向到登录页面。 然后我要调用intializeapp函数。 我对Angular和Ionic还是比较陌生,因此任何技巧都将有所帮助。 `
ionViewCanEnter() {
console.log("1");
console.log(this.userSevice.isUserLoggedIn());
if (this.userSevice.isUserLoggedIn() === false){
this.nav.push(LoginPage);
alert("user is logging in");
}
else{
this.initializeApp();
}
}`
angular中有一个漂亮的概念叫做Guard
,您可以使用它来处理您在angular应用程序中的整个登录流程。
您可以创建一个身份验证保护,并将其放置在路由器中或任何地方,并在用户未登录并尝试访问站点的私有区域时强制其进入登录页面。
这是一个简单的身份验证防护
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authService: AuthService) { }
canActivate(): Observable<boolean> | boolean {
// here check if this is first time call. If not return
// simple boolean based on user object from authService
// otherwise:
return this.authService.getAuthenticated.map(user => {
this.authService.setUser(user);
return user ? true : false;
})
}
}
在路由器中,您可以像这样使用
const routes: Routes = [
{
path: '',
canActivate: [AuthGuardService],
runGuardsAndResolvers: 'always',
}
...
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.