【问题标题】:Angular: JWT renewal with NgRx effectAngular:具有 NgRx 效果的 JWT 更新
【发布时间】:2018-09-16 16:29:03
【问题描述】:

我想将this code 改编为 Angular 项目,使用 NgRx 效果来处理 JWT 更新。但是我被 observables 卡住了,因为令牌只更新一次,因为timer 是第一次完成。我也尝试使用 delay 运算符,但由于它不创建可观察对象,我不知道如何在此流中实现它。有什么想法吗?谢谢!

// auth.effects.ts

@Effect()
renewJWT$: Observable<Action> = this.actions$.pipe(
  ofType(ActionTypes.RenewJWT),
  filter(() => this.authService.isLoggedIn()),
  concatMap(() =>
    this.authService.renewJWT().pipe(
      map(() => new ScheduleJWTRenewalAction())
    )
  )
);

@Effect()
scheduleJWTRenewal$: Observable<Action> = this.actions$.pipe(
  ofType(
    ROOT_EFFECTS_INIT,
    ActionTypes.SigninSuccess,
    ActionTypes.ScheduleJWTRenewal
  ),
  takeUntil(this.actions$.pipe(ofType(ActionTypes.Logout))),
  map(() => Date.parse(this.authService.session.expiresAt) - Date.now()),
  filter((remainingTime: number) => remainingTime > 0),
  concatMap((remainingTime: number) =>
    timer(Math.max(remainingTime, 1)).pipe(
      map(() => new RenewJWTAction())
    )
  )
);

【问题讨论】:

  • 为什么不保持使用 setTimeout 的原则,它可以调度特定的操作来更新(或检查过期)令牌。

标签: angular jwt ngrx ngrx-effects


【解决方案1】:

计时器

签名:计时器(initialDelay:数字|日期,期间:数字, 调度器:调度器):可观察的

在给定持续时间后,每隔指定时间按顺序发出数字 持续时间。

如果未指定periodtimer 将发出一次并完成。

takeUntil(this.actions$.pipe(ofType(ActionTypes.Logout))),

用户注销后scheduleJWTRenewal$ 流将完成。用户是否可以再次登录? ) .

filter上替换takeUntil会更好。

【讨论】:

    猜你喜欢
    • 2020-02-16
    • 1970-01-01
    • 2018-01-11
    • 2019-07-20
    • 2017-12-04
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多