How to save Token to Local Storage

Boba picture Boba · Dec 12, 2014 · Viewed 32.4k times · Source

I'm working on a log in system for an app in school. I can register a user that gets saved to my azure documentDB. I can then, sort of log in with the user. But it (the Token) never gets saved so that I can access the token..

The script for the log in looks like this:

      var signin = function() {

            var tokenUrl = "http://localhost:15746/Token";
            var loginData = $("#userSignup").serialize();
            loginData = loginData + "&grant_type=password";
            $.post(tokenUrl, loginData).then(navigateToEvent);

            return false;
        }

        $("#signup").click(signin);

How could I store the Token? In Local Storage? How?

Thanks in advance.

Answer

Andreas Argelius picture Andreas Argelius · Dec 12, 2014

To save a string in Local Storage you use

window.localStorage.setItem(key, value);

You can get the value later with:

window.localStorage.getItem(key);