javascriptnode.jsreactjsweb-development-serverweb-developer-toolbar

Is there a way to remember a string from a user session, even after the user has logged out, so everytime they come in, it autofills?


I want to let the user login and a few actions will give them a string, this string needs to be stored somewhere so that even after they logout and login later on, the string can get auto-filled in a text box.

Would cookies be feasible? If so kindly guide me as to how.


Solution

  • You can do it with JavaScript using localStorage.

    For example:

    localStorage.setItem('myLoginString', their_loginString);
    

    You can then fetch the value later with:

    their_loginString = localStorage.getItem('myLoginString');
    

    This is stored in their browser, so:

    1. The string would need to be generated on the browser side, or fetched from the server with an AJAX request or similar.

    2. It is unique to that browser. If they load a different one, then the saved string won't be there.