简体   繁体   English

Angular AuthGuard 可以激活

[英]Angular AuthGuard canActivate

AuthGuard blocks everything it should. AuthGuard 会阻止它应该阻止的一切。 But one undesirable thing happens when I return true.但是当我返回 true 时,会发生一件不受欢迎的事情。 I get a "test" in the console, but instead of letting me in /test, it redirects me to /.我在控制台中得到一个“测试”,但不是让我进入 /test,而是将我重定向到 /。 Why?为什么? It's RouterModule in app.module.ts:它是 app.module.ts 中的 RouterModule:

    RouterModule.forRoot([
      { path: 'test', component: HomeComponent, canActivate: [AuthGuard] },
      { path: 'account/register', component: RegisterComponent }
], { relativeLinkResolution: 'legacy' })

And it's AuthGuard:它是 AuthGuard:

import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { CookieService } from 'ngx-cookie-service';
import { Observable } from 'rxjs';
import { AuthService } from '../services/auth.service';

@Injectable()
export class AuthGuard implements CanActivate {

  constructor(private _authService: AuthService, private _cookieService: CookieService, private router: Router) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    const tokenExists: boolean = this._cookieService.check("token");
    if (tokenExists == true) {
      const token = this._cookieService.get("token");
      this._authService.isAuthenticated(token).subscribe(data => {
        if (data as any == true) {
          console.log("test");
          return true;
        }
        else if (data as any == false) {
          this.router.navigate(['account/register']);
          return false;
        }
        else {
          this.router.navigate(['account/register']);
          return false;
        }
      });
    }
    else {
      this.router.navigate(['account/register']);
      return false;
    }
  }
}

Why is this happening?为什么会这样?

You should return subscribe scope too.您也应该返回订阅 scope。 refator as重构为

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { const tokenExists: boolean = this._cookieService.check("token"); if (tokenExists == true) { const token = this._cookieService.get("token"); return this._authService.isAuthenticated(token).subscribe(data => { if (data as any == true) { console.log("test"); return true; } else if (data as any == false) { this.router.navigate(['account/register']); return false; } else { this.router.navigate(['account/register']); return false; } }); } else { this.router.navigate(['account/register']); return false; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “没有 AuthGuard 的提供者!” 在 Angular 2 中使用 CanActivate - “No provider for AuthGuard!” using CanActivate in Angular 2 角度路由可以激活AuthGuard传输查询参数 - Angular routing canActivate AuthGuard transmit query params 在 Angular 2 AuthGuard 中添加对 canActivate 的 API 调用 - Adding an API call to canActivate in Angular 2 AuthGuard Angular 2组织AuthGuard(canActivate)和AuthService代码 - Angular 2 Organize AuthGuard(canActivate) and AuthService code Angular AuthGuard canActivate with observable from Promise 不工作 - Angular AuthGuard canActivate with observable from promise not working 在 AuthGuard 的 Angular8 canActivate 方法中使用 API 验证令牌 - Verify token with API in Angular8 canActivate method of AuthGuard 登出时未呼叫Angular AuthGuard CanActivate-Firebase Auth - Angular AuthGuard CanActivate not called when signing out - Firebase Auth Session Angular 超时如果用户在 canActivate function 的 AuthGuard - Session timeout in Angular if user is idle in canActivate function of AuthGuard 如何为authguard单元测试canActivate of Angular? - How do I unit test canActivate of Angular for authguard? Angular2路由可以使用用户角色参数激活和使用AuthGuard(JWT) - Angular2 routing canActivate and AuthGuard (JWT) with user role parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM