0
votes

I have a client request where I need to show a pop up 5 min before session is expired in Spartacus, but I do not find anywhere in the framework where this is set up. Can someone please let me know where I can find it? So far I know it is 12 hours.

Thanks

2

2 Answers

0
votes

I'm assuming that you are using the default authentication flow (password flow) with the Hybris OAuth server. In this case, the session length is controlled through OAuth client settings in backoffice.

However to know when the session expires you can check the token payload (AuthStorageService.getToken). One of the properties would be expiration time which could be used to know when the session will actually end.

0
votes

Marcin is correct. Spartacus is 100% API driven, interacting with Commerce backend by sending request to configured endpoints. These endpoints require an access token to be sent with the request, and this access token needs to be retrieved by following the Client Credentials Flow that is defined by the OAuth specification.

As long as you log in successfully, you can find access token issued by Commerce backend in Chrome dev tools, application tab -> Local storage as highlighted below:

enter image description here

the field expires_at stores the value of exact date and time when token will be expired.

you can use the code below in console to convert it to human readable string:

new Date(1627660784476).toGMTString();

enter image description here

You can control the token time-to-live value via configuration in backoffice by property: oauth2.accessTokenValiditySeconds

enter image description here

See document for detail:

https://help.sap.com/viewer/d0224eca81e249cb821f2cdf45a82ace/2105/en-US/3d3ea6a4d5fa486aa324ce278fa2afc3.html?q=oauth2.accessTokenValiditySeconds

enter image description here

if you need to code in Spartacus to know when the token will be expired, inject AuthStorageService in your app.module.ts, and then access expires_at property of result returned by getToken method.

export class AppModule {
  constructor(private authService: AuthStorageService){
    const token: Observable<AuthToken> = this.authService.getToken();

    token.subscribe((token) => console.log('expire at:' , token.expires_at));
  }
}

enter image description here