I am searching for a safe way to keep the user logged in in my Progressive Web Application.
When the user installs the PWA he needs to authenticate by the traditional auth method (e-mail and password). But on the client-side I will also generate a long random key and save that in localStorage. That key will be sent to the server and saved as hash in the database.
Then when the user opens the app, the app checks if a key exists in the localStorage. If it does, the key gets authenticated against the hashed version in the database. If it matches the user gets automatically logged in.
To facilitate this I will also save the user ID in localStorage.
I will put a time-to-live of a month on the localStorage so the user has to re-authenticate sometimes.
Is this safe?