0
我正在尝试通过向存在jwt头部的后端api请求进行身份验证来检查using。如果他们是用户返回,如果没有,它将返回未经授权的。Angular + Node - 如何使用Auth Guard检查经过身份验证的用户
我遇到的问题是auth.guard被触发并返回undefined在console.log中,因为auth.service尚未完成查询。
auth.guard.ts
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) { }
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
console.log(this.authService.isAuthenticated());
if (this.authService.isAuthenticated()) {
return true;
}
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false
}
}
auth.service.ts
import { JwtService } from './jwt.service';
import { Injectable } from '@angular/core';
import { ApiService } from './api.service';
import { UserService } from './user.service';
@Injectable()
export class AuthService {
user: any;
constructor(
private apiService: ApiService,
private jwtService: JwtService,
private userService: UserService) {
this.getAuthenticatedUser().subscribe(res => {
console.log(res);
this.user = res;
}, (error) => {
this.user = null;
});
}
login(credentials) {
let endpoint = 'auth/'
return this.apiService.post(endpoint, credentials)
.map(res => {
this.jwtService.saveToken(res.data.jwtToken);
window.localStorage.setItem('user_id', res.data.user._id);
});
}
logout() {
}
isAuthenticated() {
return this.user;
}
getAuthenticatedUser() {
let endpoint = 'auth/'
return this.apiService.get(endpoint);
}
}
请告诉我是这里最好的执行实现这一目标?
我的问题是,什么阻止他人在localStorage的加入了手动loggedInfo项,将其设置为任意值。 canActivate函数一旦找到该项目就会返回true。 – KHAN
@KHAN你应该在这里使用标记!你应该检查这两个令牌和isloggedin为真 – Sajeetharan
你是什么意思,检查两者都是正确的?如果你的意思是检查它们是否都在本地存储中,那么我仍然会遇到同样的问题。本地项目的完整性可以改变/添加,所以我认为我唯一的选择是每次打电话给后端... – KHAN