Angular Universal - Auth Guard 待处理问题

问题描述

我正在使用带有 Angular Material 的弹出式登录,当我添加 Angular Universal 时,身份验证保护是问题所在。 如果某些路由受 auth 保护,则页面只是开始挂起并且永远不会完成。没有 Angular Universal 的行为是在页面重新加载时它只是打开弹出窗口进行登录。

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(readonly auth: AuthService,public router: Router,private dialog: MatDialog,private store: Store<fromAuth.State>) {}

    /** Performs the user authentication prompting the user when neeed or resolving to the current authenticated user otherwise */

    public authenticate(action: loginAction = 'signIn') {
      return this.store.pipe(
        select(fromAuth.selectAuthState),take(1),switchMap(user => !user.user ? this.prompt(action) : of(user.user))
      ).toPromise();
    }

  public prompt(data: loginAction = 'signIn'): Promise<any> {

    return this.dialog.open<LogInComponent,loginAction>(LogInComponent,{ data }).afterClosed().toPromise();
  }

  canActivate(route: ActivatedRouteSnapshot,state: RouterStateSnapshot) {
    // Gets the authorization mode when specified
    // const mode = route.queryParamMap.get('authMode') || 'signIn';
    // Prompts the user for authentication
     return this.authenticate()
      .then(user => !!user);
  }
}

如果我直接访问 canActivate 中的 ngrx 商店,它可以工作,但我想使用 .toPromise()

我正在使用 httponly cookie,并且在每次重新加载时,Angular 都会向 nodejs db 发送 http 请求以获取用户数据。在其他所有路线上它都按预期工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)