- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用 angular2 rc.3 和 @angular/router": "3.0.0-alpha.7
我的授权服务器
export class AuthService {
isLogged:boolean;
constructor(private principal:PrincipalService){
this.authenticated();
}
authenticated() {
this.principal.currentUser().subscribe(
e=> {
if (!!e && e.authenticated) {
this.isLogged = true;
}
this.isLogged = false;
},
error=> {
this.isLogged = false;
}
)
}
授权 guard :
constructor(private auth:AuthService ) {}
canActivate(next: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean{
// here the **auth.isLogged** is undefined !! why?**
if(this.auth.isLogged){
return true;
}
else {
window.location.href='/login';
return false;
}
我设置了路由器
{
path: 'property',
component: PropertyHomeComponent,
canActivate: [AuthGuard]
}
当我打开“/property”页面时,AuthGuard 无法读取“this.auth.isLogged”,并且没有重定向到“/login”页面,
如何解决?
最佳答案
我找到了革命。 (感谢@Michael 的提示)
AuthGuard
return this.principal.currentUser().map(e=> {
if (e) {
return true;
}
}).catch(()=> {
window.location.href = ConstantService.loginUrl;
return Observable.of(false)
})
}
关于angular - 'authGuard' 中的 canActivate 无法读取 AuthServer 的 ' observable current user ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036113/
我有一个 AuthGuard.service.ts 的代码如下。 import { Injectable } from '@angular/core'; import { ActivatedRoute
我想通过检查用户是否从服务器登录但异步函数未执行来保护我的路由 这是我的代码: canActivate (route: ActivatedRouteSnapshot , state: RouterSt
我正在使用 Angular CanActivate Authguard 接口(interface)来保护我的组件。 @Injectable() export class AuthGuard imple
要允许访问管理路由,我必须检查两件事: 如果用户通过身份验证 如果此用户是管理员。我从 firebase 数据库获取管理状态。 管理 guard import {ActivatedRouteSnaps
我在这里阅读了指南:https://angular.io/docs/ts/latest/guide/router.html 看起来很简单,但是,我不确定如何在 auth guard (canActiv
我想创建一个路由防护来保护路由免受未经授权的用户的侵害。 我正在使用 jsonwebtoken 进行授权,目前将其存储在 localStorage 中。 我的想法是,当用户想要访问 protected
我使用 ngx admin 创建使用 nebular 的管理面板,我按照此文档添加 authGard: docs 这用于自定义登录: docs2 所有事情都正常,我收到成功消息: 但我在 autoga
I'm loading a books module with the following routing configuration (src/app/index.ts) -- 请注意,这是一个 s
我正在尝试在 Angular 5 中创建条件路由,我已经看到了这篇文章和 Angular 的官方里程碑页面: Angular2 conditional routing 。 但是我没有找到根据输入变量创
页面刷新后,身份验证似乎不会持续存在。每当我登录时,它都会成功重定向我。但是,当我刷新时,无法再访问。 这是auth guard 路由器 import { BrowserModule } from '
我的应用程序将使用两种不同的身份验证策略 - 一种针对使用浏览器的用户,另一种针对公共(public) API。我将为使用浏览器的用户设置一个 header,然后我的应用程序将根据该 header 的
有没有办法在 authguard 中使用相对路径模式进行重定向? 我试过 @Injectable() export class ServerAuthGuard implements CanActiva
我有这个 AuthGuard: export class AuthGuard implements CanActivate { constructor (private router: Route
尝试遵循此处提供的 AuthGuard 示例: http://www.sparkbit.pl/angular-2-route-guards-real-life-example/ 不幸的是,在尝试实现
在模板组件中AppComponent ,根据值,变量 this.loggedInService.isLoggedIn在 logIn() 之间切换和logout()方法,在应用程序组件 AppCompo
我正在开发一个 Angular 应用程序,它具有以下URL路径, /(根目录) /类(class) /类(class)/创建 /类(class)/:id /验证/登录 我已为除 auth/login
我有一个 ionic 4 应用程序,它本地存储有关用户的信息。当用户打开应用程序时,如果他的信息在那里,那么我希望将他发送到主页。如果他不是,那么我希望他被路由到登录页面,这很正常。 我遇到的问题是,
使用 ASP.NET Core API 应用程序登录到 Angular 应用程序后,出现以下错误 Error: Uncaught (in promise): Error: StaticInjector
我想这是一个很简单的问题,但不幸的是我真的不知道如何处理它。 我正在尝试将我的 UserAuthenticationService 服务与 ActivationGuard 连接。 UserAuthen
我想在以 Angular 授予对特定路由的访问权限之前提供服务器端身份验证。 我有一个实现 CanActivate 的 AuthGuard 和一个服务 AuthService。authService
我是一名优秀的程序员,十分优秀!