Skip to content

Instantly share code, notes, and snippets.

@probil
Last active June 14, 2017 09:54
Show Gist options
  • Save probil/fb1fc115ca717b64af2c80d3b912943a to your computer and use it in GitHub Desktop.
Save probil/fb1fc115ca717b64af2c80d3b912943a to your computer and use it in GitHub Desktop.
Store `hapi-swagger` auth key in `localStorage`
const HapiSwagger = require('hapi-swagger');
const config = require('../utils/config');
const swaggerOptions = {
basePath : `/api/${config.service.apiversion}/`,
jsonEditor : true,
documentationPath : '/api/docs',
swaggerUIPath : '/api/docs/swaggerui/',
jsonPath : '/api/docs/swagger.json',
securityDefinitions: {
default: {
type : 'apiKey',
description: 'JWT token',
name : 'Authorization',
in : 'header'
}
},
uiCompleteScript: `
var lsKey = '__AUTH-TOKEN__';
var $inputApiKey = $('#input_apiKey');
// Recover accessToken from localStorage if present.
var key = window.localStorage && window.localStorage.getItem(lsKey);
if (key) {
$inputApiKey.val(key);
// update auth
addApiKeyAuthorization();
}
$inputApiKey.change(function () {
var key = $inputApiKey[0].value;
if (key && key.trim() !== '') {
// Save this token to localStorage if we can to make it persist on refresh.
window.localStorage && window.localStorage.setItem(lsKey, key);
}
else {
window.localStorage && window.localStorage.removeItem(lsKey);
}
});
`
};
module.exports = {
register: HapiSwagger,
options : swaggerOptions
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment