【问题标题】:Angular AuthGuard canActivateAngular AuthGuard 可以激活
【发布时间】:2021-03-06 19:34:04
【问题描述】:

AuthGuard 会阻止它应该阻止的一切。但是当我返回 true 时,会发生一件不受欢迎的事情。我在控制台中得到一个“测试”,但不是让我进入 /test,而是将我重定向到 /。为什么? 是 app.module.ts 中的 RouterModule:

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

它是 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;
    }
  }
}

为什么会这样?

【问题讨论】:

    标签: angular authentication auth-guard


    【解决方案1】:

    您也应该返回订阅范围。 重构为

     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;
        }
      }

    【讨论】:

    • 它不工作。回报?这将如何运作?
    • 订阅者范围是一个异步线程区域,它不与您的 canActivate 范围组合,因此您必须返回订阅范围。真正的问题必须是运行时原因,因为我看不到这种语法有任何问题。所以如果你在控制台上遇到错误日志,你能把它发送到这里吗?
    • 我无法编译这个。它有两个错误:错误 TS2322 (TS) 类型“订阅”不可分配给类型“布尔 |网址树 |可观察 |承诺'。 “订阅”类型缺少“承诺”类型中的以下属性': then, catch, [Symbol.toStringTag], finally and Error TS2322 Build:Type 'Subscription' is notassignable to type 'boolean |网址树 |可观察 |承诺'.
    • : 可观察 重写为:Observable
    • 它什么也没做。我做了':任何',现在它与没有返回的效果相同:/
    猜你喜欢
    • 1970-01-01
    • 2018-06-09
    • 2023-02-20
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多