刷新令牌是否需要在 Spartacus 中显式设置?

问题描述

在我的 Sparatacus 2.0 应用程序中,我的 API 在 access_token 超时后失效并出现此错误

 {
    "error":"invalid_token","error_description":"Invalid access token: 14399ab4-2e63-4ee2-a0a8-3ff493e09142"
 }

登录后的认证响应是这样的

{
  "access_token" : "14399ab4-2e63-4ee2-a0a8-3ff493e09142","token_type" : "bearer","refresh_token" : "5c28d5b1-c978-4e38-adfa-c1e41ef97b9c","expires_in" : 13,"scope" : "basic openid"
}

我是否需要通过调用一些 OOTB Spartacus 方法在前端显式设置刷新令牌?

解决方法

我在 Spartacus 3.0 中遇到了类似的问题。只要登录后我在同一个浏览器选项卡中,刷新令牌就会被存储(在内存中),当我刷新页面时,用户只在访问令牌过期之前进行身份验证。

我的问题是 AuthStatePersistenceService 没有将刷新令牌保存在本地存储中,因此在页面重新加载后它不再存在。我能够通过覆盖此服务中的 getAuthState 函数来修复它:

import { combineLatest,Observable } from 'rxjs';
import { filter,map } from 'rxjs/operators';

import { Injectable } from '@angular/core';
import { AuthStatePersistenceService,SyncedAuthState } from '@spartacus/core';

@Injectable()
export class MyAuthStatePersistenceService extends AuthStatePersistenceService {
  /**
   * Gets and transforms state from different sources into the form that should
   * be saved in storage.
   */
  protected getAuthState(): Observable<SyncedAuthState> {
    return combineLatest([
      this.authStorageService.getToken().pipe(
        filter((state) => !!state),map((state) => {
          return {
            ...state,};
        }),),this.userIdService.getUserId(),this.authRedirectStorageService.getRedirectUrl(),]).pipe(
      map(([authToken,userId,redirectUrl]) => {
        let token = authToken;

        if (token) {
          token = { ...token };
        }

        return { token,redirectUrl };
      }),);
  }
}

也许对你有帮助。

,

斯巴达克斯存在一个问题,现已修复。

如果您想解决此问题,只需更新 https://github.com/SAP/spartacus/pull/4443/commits/0cbf839ca4b6dda195730a81fcce5881bc70be39#diff-f13978c59296903fbcc824e50a0563fbf3dc1583b818c3769d0e1f84fc99d036 根据这个提交。