O método de armazenamento padrão do Spartacus é localStorage
que 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 AuthStatePersistenceService
o initSync
mé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
}
]