Skip to content

Instantly share code, notes, and snippets.

@rubensa
Forked from miguelmota/index.html
Created November 3, 2023 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubensa/112961ea94452b894a43d93c29d99ab5 to your computer and use it in GitHub Desktop.
Save rubensa/112961ea94452b894a43d93c29d99ab5 to your computer and use it in GitHub Desktop.
Swagger UI authorization header bearer access token
<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