【发布时间】:2023-01-26 12:02:45
【问题描述】:
我在刷新页面后注销时遇到问题。我认为问题出在获取当前用户的方法或检查用户管理员的方法中。
admin-auth-guard.service.ts
export class AdminAuthGuardService implements CanActivate {
constructor(
private router: Router,
private userService: UserService,
) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.checkLogin();
}
checkLogin(): Observable<boolean> {
return this.userService.getCurrentUser().pipe(
map(user => {
if (user) {
const isUserAdmin = this.userService.isRoleAdmin();
if (isUserAdmin) {
return true;
} else {
this.router.navigate(['/forbidden']);
return false;
}
} else {
this.router.navigate(['/login']);
return false;
}
})
);
}
}
用户服务.ts
导出类用户服务{
当前用户!:用户 |无效的;
private usersUrl = `${SERVER_API_URL}/api/user`;
私人 apiServerUrl = environment.apiBaseUrl;
构造函数(私有 http: HttpClient){ }
getCurrentUser(): 可观察的 {
返回 this.http.get(`${this.usersUrl}/current-user`);
}
isRoleAdmin(): 布尔值 {
如果(this.currentUser){
返回 this.currentUser.authorities.some((authority: string) => authority === Authority.ADMIN);
} 别的 {
返回假;
}
}
isRoleUser(): 布尔值 {
如果(this.currentUser){
返回 this.currentUser.authorities.some((authority: string) => authority === Authority.USER);
} 别的 {
返回假;
}
}
isRoleDeleter(): 布尔值 {
如果(this.currentUser){
返回 this.currentUser.authorities.some((authority: string) => authority === Authority.DELETER);
} 别的 {
返回假;
}
}
getStudentsData(userId: number): 可观察{
返回 this.http.get(`${this.apiServerUrl}/api/user/edit/${userId}`);
}
updateUser(profesor: Profesors[], userId: number): Observable{
return this.http.put(`${this.apiServerUrl}/api/user/update/${userId}`, 教授);
}
}
登录组件.ts
导出类 LoginComponent 实现 OnInit {
验证=假; // 显示加载
登录失败=假; // 显示登录失败信息
用户凭据!:用户凭据;
private loggedIn = new BehaviorSubject(localStorage.getItem("isLoggedIn") === "true");
构造函数(
私人登录服务:登录服务,
私人路由器:路由器,
私有用户服务:UserService
) {
}
ngOnInit(): void {
this.userCredentials = new UserCredentials();
}
登录() {
this.authenticating = true;
this.loginFailed = false;
this.loginService.authenticate(this.userCredentials).subscribe(
(jwtToken: JwtToken) => this.successfulLogin(jwtToken),
() => this.loginFailed = true
).add(() => this.authenticating = false);
}
成功登录(jwtToken:JwtToken){
localStorage.setItem('token', jwtToken.token); // 将令牌值存储到本地存储
this.userService.getCurrentUser().subscribe((currentUser: User) => this.userService.currentUser = currentUser);
this.router.navigate(['/home']);
}
isUserLoggedIn(): 布尔值 {
返回!this.userService.currentUser;
}
}
用户.model.ts
出口类用户{
身份证号;
用户名:字符串;
名字:字符串;
姓氏:字符串;
权限:字符串[];
构造函数(id: number, username: string, firstName: string, lastName: string, authorities: string[]){
这个.id = id;
this.username = 用户名;
this.firstName = firstName;
this.lastName = lastName;
this.authorities = 权威;
}
}
【问题讨论】:
-
根据您获取当前用户的方式,如果用户未连接,它可能会返回 403。结果,Observable 将失败。你能检查开发工具中的网络选项卡吗?
-
当前用户在网络中返回 200
-
您还可以在此处包括
User类型吗?它是一个接口,还是一个类?我的想法是确保User是一个类,并在那里实现isRoleAdmin、isRoleUser和isRoleDeleter。这样你也可以删除功能羡慕代码味道也是如此。然后,您将能够在您已有的user实例上调用这些方法,而不是依赖UserService为您执行此操作。当您检查角色时,很可能UserService.currentUser未初始化。 -
用户是具有构造函数的类
-
你在代码中的任何地方调用这个构造函数吗?我没有看到这个构造函数的任何用法
标签: angular authentication angular-material typescript2.0