The default storage method of Spartacus is localStorage
that we can replace it with SessionStorage
.
Default implementation: 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),
})
);
}
Create a new one CustomAuthStatePersistenceService
, overloading AuthStatePersistenceService
the initSync
method of :
@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 类型
})
);
}
}
Then replace in the providers
area :
providers: [
{
provide: AuthStatePersistenceService,
useExisting: CustomAuthStatePersistenceService
}
]