angular - 我需要创建效果,它将在时间间隔内使用刷新 token 刷新访问 token

标签 angular ngrx refresh-token

我想创建一个效果,它将在用户成功登录后按时间间隔刷新 token ,我还需要从商店中选择刷新 token ,这对我来说开始太复杂了,我不知道该怎么做.

目前效果

  @Effect()
  refreshAccessToken$ =
    this.actions$.pipe(
      ofType(AccountActionsTypes.AccountLoginSuccess),
      map(() => {
        interval(5000)
          .pipe(
            mapTo(() => this.store
              .pipe(
                select(selectRefreshToken),
                map(refreshToken => {
                  if (refreshToken != '' && refreshToken != undefined) {
                    this.accountService.refreshToken(refreshToken)
                      .pipe(
                        map((response: { token: string }) => {
                          return {type: AccountActionsTypes.RefreshTokenSuccess, token: response.token};
                        })
                      );
                  } else {
                    return {type: AccountActionsTypes.RefreshTokenSuccess, token: ''};
                  }
                })
              )));
      }));

但是我收到这个错误 enter image description here

也许有人会发现这很有帮助,这是通过建议修复的效果:

@Effect()
  refreshAccessToken$ =
    this.actions$.pipe(
      ofType(AccountActionsTypes.AccountLoginSuccess),
      switchMap(() => {
        return interval(5000)
          .pipe(
            switchMap(() => this.store
              .pipe(
                select(selectRefreshToken),
                switchMap((refreshToken) => this.accountService.refreshToken(refreshToken)
                  .pipe(
                    map((response: { token: string, status: string }) => {
                      if (response.token !== undefined) {
                        return {
                          type: AccountActionsTypes.RefreshTokenSuccess,
                          token: response.token,
                        };
                      } else {
                        return {type: AccountActionsTypes.RefreshTokenWrong, status: response.status};
                      }
                    })
                  ))
              )));
      }));

最佳答案

如果要返回内部可观察值,则必须使用展平运算符:

 @Effect()
  refreshAccessToken$ =
    this.actions$.pipe(
      ofType(AccountActionsTypes.AccountLoginSuccess),
      // 👇👇👇
      switchMap(() => { ... })

关于angular - 我需要创建效果,它将在时间间隔内使用刷新 token 刷新访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63203377/

相关文章:

node.js - Angular 5 反复出现问题 - Webpack 无法编译简单项目

angular - ngrx 商店选择器在从自定义库导入应用程序时失败

angular - NgRx - 状态如何组合和初始化

使用 Shiny 从 Azure 刷新 token

Angular 2 Universal - 使用手写笔和哈巴狗模板进行服务器端渲染

Angular 解析器获取 Observable

Angular CLI - 检测库更改以重建消费应用程序

angular - 使用 OnPush 策略时不会重新呈现子组件的 View

authentication - 如何使用 OAuth2 静默刷新过期的 JWT token ?

c# - 如何在 asp.net 中生成刷新 token ?