-
-
Save rubensa/112961ea94452b894a43d93c29d99ab5 to your computer and use it in GitHub Desktop.
Swagger UI authorization header bearer access token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
window.onload = function() { | |
fetch('./swagger.json') | |
.then(function(response) { | |
response.json() | |
.then(function(json) { | |
json.schemes[0] = window.location.protocol.slice(0, -1) | |
json.host = window.location.host | |
const ui = SwaggerUIBundle({ | |
spec: json, | |
dom_id: '#swagger-ui', | |
deepLinking: true, | |
presets: [ | |
SwaggerUIBundle.presets.apis | |
], | |
plugins: [ | |
SwaggerUIBundle.plugins.DownloadUrl | |
], | |
requestInterceptor: function(req) { | |
req.headers.Authorization = 'Bearer ' + localStorage.getItem('accessToken') | |
return req | |
} | |
}) | |
window.ui = ui | |
addAuthButton() | |
}) | |
}) | |
} | |
function addAuthButton() { | |
var container = document.querySelector('.information-container') | |
var div = document.createElement('div') | |
var button = document.createElement('button') | |
button.appendChild(document.createTextNode('Authorization')) | |
button.style = 'font-size: 14px; font-weight: 700; min-width: 80px; padding: 6px 15px; text-align: center; border-radius: 3px; background: #41cd8e; text-shadow: 0 1px 0 rgba(0,0,0,.1); font-family: Titillium Web,sans-serif; color: #fff; border: none;' | |
button.addEventListener('click', function(event) { | |
event.preventDefault() | |
var token = prompt('Enter JWT access token token. Eg. eyJraW...qJSaxQ') | |
if (!token) { | |
localStorage.removeItem('accessToken') | |
alert('Token cleared') | |
} else { | |
token = token.replace(/[^\d\w_.-]/gi, '') | |
localStorage.setItem('accessToken', token) | |
} | |
}) | |
container.appendChild(div.appendChild(button)) | |
} | |
<script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment