如何更改 Spartacus 默认的 token 存储方式

Spartacus 认的存储方式是 localStorage,我们可以采取二次开发的方式,将其替换成 SessionStorage.

认的实现:AuthStatePersistenceService

/**
   * Initializes the synchronization between state and browser storage.
   */
  public initSync() {
    this.subscription.add(
      this.statePersistenceService.syncWithStorage({
        key: this.key,
        state$: this.getAuthState(),
        onRead: (state) => this.onRead(state),
      })
    );
  }

新建一个 CustomAuthStatePersistenceService,重载标准的 AuthStatePersistenceServiceinitSync 方法

@Injectable({ providedIn: 'root' })
export class CustomAuthStatePersistenceService extends AuthStatePersistenceService {
 
  initSync() {
    this.subscription.add(
      this.statePersistenceService.syncWithStorage({
        key: this.key,
        state$: this.getAuthState(),
        onRead: (state) => this.onRead(state),
        storageType: StorageSyncType.SESSION_STORAGE, // 此处传入自定义的 storage 类型
      })
    );
  }
}

然后在 providers 区域进行替换:

providers: [
  {
    provide: AuthStatePersistenceService,
    useExisting: CustomAuthStatePersistenceService
  }
]

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...
win11本地账户怎么改名?win11很多操作都变了样,用户如果想要...