简体   繁体   English

Angular Universal:使用 NgRx 在服务器端获取 Store in Guard

[英]Angular Universal: Get Store in Guard on Server-side using NgRx

I am running an Angular 9 Universal application that uses NgRx for state management.我正在运行一个 Angular 9 通用应用程序,它使用 NgRx 进行 state 管理。 I am also using ngrx-store-localstorage to persist the Store into the user's localStorage.我还使用 ngrx-store-localstorage 将 Store 持久化到用户的 localStorage 中。

I am trying to check if user is loggedIn before accessing certain routes with NgRx inside a Guard:我正在尝试在使用 Guard 中的 NgRx 访问某些路由之前检查用户是否已登录:

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {

    if (isPlatformBrowser(this.platformId)) {
      console.log('browser')
      return this.store.select('user').pipe(
        map(authUser => {
          if (!authUser.id || !authUser.token) {
            console.log(authUser)
            this.router.navigate(['/login'])
            return false
          }
          return authUser.id ? true : false;
        }))
    }
    console.log('server')
    this.router.navigate(['/login'])
    return false
  }

I am doing a platform check since my server does not have access to the store.我正在进行平台检查,因为我的服务器无权访问商店。 But this creates unwanted behaviors since it checks twice and sometimes render the login page before rendering the guarded page.但这会产生不需要的行为,因为它会检查两次,有时会在呈现受保护的页面之前呈现登录页面。

Do you know a way to let my server know about the current State or an alternative way to verify if my user is authenticated?您是否知道让我的服务器了解当前 State 的方法或验证我的用户是否已通过身份验证的替代方法?

I ended up using ngx-cookie plugin that makes my guard able to access cookies on the Browser and on the Server: https://github.com/ngx-utils/cookies#servermodulets我最终使用了 ngx-cookie 插件,它使我的警卫能够在浏览器和服务器上访问 cookies: https://github.com/ngx-utils/cookies#servermodulets

My guard looks like that now:我的后卫现在看起来像这样:

    canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
        if (isPlatformBrowser(this.platformId)) {
          var token = this.cookies.getObject('_avoSession')
          if (!token) {
            this.router.navigate(['/login'])
            return false
          }
          console.log(token)
          return true

        }
        if (isPlatformServer(this.platformId)) {
          let cookie = this.cookies.getObject('_avoSession')
          if (!cookie) {
            this.router.navigate(['/login'])
            return false
          }
          return true 
        }

  }

The platformCheck here is useless since both my server and browser have access to cookies, but if you need some modularity and add specific checks for different use cases, it can be useful.此处的 platformCheck 没用,因为我的服务器和浏览器都可以访问 cookies,但是如果您需要一些模块化并为不同的用例添加特定的检查,它会很有用。

You can also verify your jwt by following this example here with Observables.您还可以按照此处的示例和 Observables 来验证您的 jwt。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM