微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何更改 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
  }
]

原文地址:https://cloud.tencent.com/developer/article/2179969

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐