Como alterar o método de armazenamento de token padrão do Spartacus

O método de armazenamento padrão do Spartacus é localStorageque podemos substituí-lo por SessionStorage.

Implementação padrão: 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),
      })
    );
  }

Crie um novo CustomAuthStatePersistenceService, sobrecarregando AuthStatePersistenceServiceo initSyncmétodo padrão de:

@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 类型
      })
    );
  }
}

Em seguida, substitua na providersárea :

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

Acho que você gosta

Origin blog.csdn.net/i042416/article/details/124415565
Recomendado
Clasificación