Save accessToken in localStorage. Fixes #47

This commit is contained in:
Samuel Reed 2014-11-10 10:11:25 +00:00
parent e9d990bf11
commit ce76f17ee1
1 changed files with 16 additions and 2 deletions

View File

@ -3,9 +3,10 @@
// Refactoring of inline script from index.html. // Refactoring of inline script from index.html.
/*global SwaggerUi, log, ApiKeyAuthorization, hljs, window, $ */ /*global SwaggerUi, log, ApiKeyAuthorization, hljs, window, $ */
$(function() { $(function() {
var lsKey = 'swagger_accessToken';
$.getJSON('config.json', function(config) { $.getJSON('config.json', function(config) {
log(config); log(config);
loadSwaggerUi(config); loadSwaggerUi(config);
}); });
var accessToken; var accessToken;
@ -35,6 +36,14 @@ $(function() {
$('#api_selector').submit(setAccessToken); $('#api_selector').submit(setAccessToken);
$('#input_accessToken').keyup(onInputChange); $('#input_accessToken').keyup(onInputChange);
// Recover accessToken from localStorage if present.
if (window.localStorage) {
var key = window.localStorage.getItem(lsKey);
if (key) {
$('#input_accessToken').val(key).submit();
}
}
window.swaggerUi.load(); window.swaggerUi.load();
} }
@ -49,6 +58,11 @@ $(function() {
accessToken = key; accessToken = key;
$('.accessTokenDisplay').text('Token Set.').addClass('set'); $('.accessTokenDisplay').text('Token Set.').addClass('set');
$('.accessTokenDisplay').attr('data-tooltip', 'Current Token: ' + key); $('.accessTokenDisplay').attr('data-tooltip', 'Current Token: ' + key);
// Save this token to localStorage if we can to make it persist on refresh.
if (window.localStorage) {
window.localStorage.setItem(lsKey, key);
}
} }
} }