- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想这是一个很简单的问题,但不幸的是我真的不知道如何处理它。
我正在尝试将我的 UserAuthenticationService
服务与 ActivationGuard
连接。
UserAuthenticationService.ts
:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
@Injectable()
export class UserAuthenticationService {
isUserAuthenticated: boolean = false;
username: string;
constructor(private http: Http) {
}
authentication() {
this.http.get(`http://localhost/api/auth/isLogged/${this.username}`)
.subscribe(res => { //^^returns true or false, depending if the user is logged or not
this.isUserAuthenticated = res.json();
},
err => {
console.error('An error occured.' + err);
});
}
}
ActivationGuard.ts
import {Injectable} from '@angular/core';
import {Router, RouterStateSnapshot, ActivatedRouteSnapshot} from '@angular/router';
import {Observable} from 'rxjs/Observable';
import {UserAuthenticationService} from './UserAuthenticationService';
interface CanActivate {
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean
}
@Injectable()
export class WorksheetAccessGuard implements CanActivate {
constructor(private router: Router, private userService: UserAuthenticationService) {
}
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.userService) {
this.router.navigate(['/']);
return false;
}
return true;
}
}
如果我只使用 localStorage
来存储用户登录与否的信息,效果很好:
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (!localStorage.getItem('currentUser')) {
this.router.navigate(['/']);
return false;
}
return true;
}
但是如何将服务与守卫连接起来呢?期待任何形式的帮助。提前谢谢你。
如果您需要更多信息,请告诉我,我将编辑我的帖子。
最佳答案
在构造函数中或在 ngOnit 中调用 UserAuthenticationService 的 authentication() 方法,然后它设置 isUserAuthenticated 变量并在 ActivationGuard.ts 中使用它
UserAuthenticationService.ts:
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
@Injectable()
export class UserAuthenticationService {
isUserAuthenticated: boolean = false;
username: string;
constructor(private http: Http) {
this.authentication();
}
authentication() {
this.http.get(`http://localhost/api/auth/isLogged/${this.username}`)
.subscribe(res => { //^^returns true or false, depending if the user is logged or not
this.isUserAuthenticated = res.json();
},
err => {
console.error('An error occured.' + err);
});
}
}
ActivationGuard.ts
@Injectable()
export class WorksheetAccessGuard implements CanActivate {
constructor(private router: Router, private userService: UserAuthenticationService) {
}
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.userService.isUserAuthenticated) {
this.router.navigate(['/']);
return false;
}
return true;
}
}
关于javascript - 将身份验证服务与 AuthGuard 连接(简单问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43657760/
我有一个 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
我是一名优秀的程序员,十分优秀!