Persistent user session using AWS Cognito in Electron

Viewed 1080

Making a AWS Cognito user session persistent in Electron

AWS Cognito stores the current user session in the localStorage. While the user session is present in localStorage after the user has logged in, localStorage isn't persistant in Electron - so when the application restarts, the user session is gone and the user has to log in again. Normally the user session would be recieved from localStorage, since it is persistent in browsers.

I know that one can use electron-json-storage to store data persistantly in Electron and generally in node applications, but since AWS cognito uses localStorage, I am only able to use an approach that would change localStorage into being persistant, i.e redirected to a file storage.

I have tried node-localstorage which works, but cognito still uses the localStorage from the browser, even when global.localStorage is set.

I know that the userPool can be passed a {Storage: ...} object to use that as storage, but it still used the original localStorage, when I passed the node-localstorage to it.

TLDR;

  • How can I make the AWS Cognito user session persistent in electron?
  • If possible, can I replace localStorage with node-localstorage globally, so that AWS Cognito will use it?
1 Answers

Cognito will be giving you three tokens idToken, accessToken and refreshToken A simple idea will be to save the refreshToken in your localstorage that you think is persistence. and on electron app start use that refreshToken to authenticate user user on Cognito.

Related