身份验证服务总是 return false
Authentication service always return false
我的身份验证服务几乎可以正常工作,但问题是我正在从异步 http 请求获取用户是否已登录的信息。
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 ActivationGuard implements CanActivate {
constructor(private router: Router, private userService: UserAuthenticationService) {
}
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.userService.isUserAuthenticated == false) {
this.router.navigate(['/']);
return false;
}
return true;
}
}
UserAuthenticationService.ts
import {Injectable, OnInit} from '@angular/core';
import {Http} from '@angular/http';
@Injectable()
export class UserAuthenticationService implements OnInit {
isUserAuthenticated: boolean = false;
username: string = 'admin';
constructor(private http: Http) {}
ngOnInit(){
this.http.get(`http://localhost/api/auth/isLogged/${this.username}`)
.subscribe(res => {
this.isUserAuthenticated = res.json();
},
err => {
console.error('An error occured.' + err);
});
}
}
每次我都收到 false
(即使我已登录,它也不允许我进入该应用程序)。
但是 如果我将 isUserAuthenticated: boolean = false;
行从 UserAuthenticationService.ts
文件更改为 true
- 它永远不会把我踢出去 - isUserAuthenticated
总是正确的。
可能是http请求导致的,是异步的?或者也许有更好的方法来处理它?
期待任何帮助。谢谢。
您不仅可以 return 布尔值,还可以在 canActivate 方法中使用 Observable 或 Promise:
ActivationGuard.ts
import { Injectable } from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from "rxjs/Observable";
import {UserAuthenticationService} from './UserAuthenticationService';
@Injectable()
export class ActivationGuard implements CanActivate {
constructor(private router: Router, private userService: UserAuthenticationService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.userService.isUserAuthenticated
.do(success => {
if (!success) {
this.router.navigate(['/']);
}
});
}
}
另外,让我们将 isUserAuthenticated 设置为可观察对象,这样 ActivationGuard 将订阅此管道:
UserAuthenticationService.ts
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from "rxjs/Observable";
@Injectable()
export class UserAuthenticationService {
public isUserAuthenticated:Observable<boolean>;
username: string = 'admin';
constructor(private http: Http) {
this.isUserAuthenticated = this.http.get(`http://localhost/api/auth/isLogged/${this.username}`)
.map(res => res.json())
.share();
}
}
我的身份验证服务几乎可以正常工作,但问题是我正在从异步 http 请求获取用户是否已登录的信息。
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 ActivationGuard implements CanActivate {
constructor(private router: Router, private userService: UserAuthenticationService) {
}
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.userService.isUserAuthenticated == false) {
this.router.navigate(['/']);
return false;
}
return true;
}
}
UserAuthenticationService.ts
import {Injectable, OnInit} from '@angular/core';
import {Http} from '@angular/http';
@Injectable()
export class UserAuthenticationService implements OnInit {
isUserAuthenticated: boolean = false;
username: string = 'admin';
constructor(private http: Http) {}
ngOnInit(){
this.http.get(`http://localhost/api/auth/isLogged/${this.username}`)
.subscribe(res => {
this.isUserAuthenticated = res.json();
},
err => {
console.error('An error occured.' + err);
});
}
}
每次我都收到 false
(即使我已登录,它也不允许我进入该应用程序)。
但是 如果我将 isUserAuthenticated: boolean = false;
行从 UserAuthenticationService.ts
文件更改为 true
- 它永远不会把我踢出去 - isUserAuthenticated
总是正确的。
可能是http请求导致的,是异步的?或者也许有更好的方法来处理它?
期待任何帮助。谢谢。
您不仅可以 return 布尔值,还可以在 canActivate 方法中使用 Observable 或 Promise:
ActivationGuard.ts
import { Injectable } from '@angular/core';
import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
import {Observable} from "rxjs/Observable";
import {UserAuthenticationService} from './UserAuthenticationService';
@Injectable()
export class ActivationGuard implements CanActivate {
constructor(private router: Router, private userService: UserAuthenticationService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.userService.isUserAuthenticated
.do(success => {
if (!success) {
this.router.navigate(['/']);
}
});
}
}
另外,让我们将 isUserAuthenticated 设置为可观察对象,这样 ActivationGuard 将订阅此管道:
UserAuthenticationService.ts
import {Injectable} from '@angular/core';
import {Http} from '@angular/http';
import {Observable} from "rxjs/Observable";
@Injectable()
export class UserAuthenticationService {
public isUserAuthenticated:Observable<boolean>;
username: string = 'admin';
constructor(private http: Http) {
this.isUserAuthenticated = this.http.get(`http://localhost/api/auth/isLogged/${this.username}`)
.map(res => res.json())
.share();
}
}