Skip to content

Commit

Permalink
feat: support multiple authorisations to be stored and restored (#2311)
Browse files Browse the repository at this point in the history
| Q | A |

|---------------|---------------------------------------------------------------------------------------------------------------------------|
| Bug fix? | maybe |
| New feature? | maybe <!-- please update src/**/CHANGELOG.md files -->
|
| Deprecations? | no <!-- please update UPGRADE-*.md and
src/**/CHANGELOG.md files --> |
| Issues | - <!-- prefix each issue number with "Fix #", no need to
create an issue if none exists, explain below instead --> |

<!--
Replace this notice by a description of your feature/bugfix.
This will help reviewers and should be a good start for the
documentation.

Additionally:
 - Always add tests and ensure they pass.
- For new features, provide some code snippets to help understand usage.
-->

I have an API description which has multiple authorisation options,
which is supported fine by the Swagger interfaces. However, the
store/restore functionality always overrides the previous stored value,
so only the last input is restored on page load. This change solves
that.

As far as I know everything I used should work fine on relevant
browsers.
  - Object.keys: https://caniuse.com/mdn-javascript_builtins_object_keys
  - forEach: https://caniuse.com/mdn-javascript_builtins_array_foreach

I do not believe that the build failures are related to this PR....

---------

Co-authored-by: Djordy Koert <[email protected]>
  • Loading branch information
bobvandevijver and DjordyKoert authored Oct 17, 2024
1 parent 748c6e0 commit be67a3a
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions public/init-swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,52 @@ function loadSwaggerUI(userOptions = {}) {

const storageKey = 'nelmio_api_auth';

// if we have auth in storage use it
if (sessionStorage.getItem(storageKey)) {
try {
ui.authActions.authorize(JSON.parse(sessionStorage.getItem(storageKey)));
} catch (ignored) {
// catch any errors here so it does not stop script execution
function getAuthorizationsFromStorage() {
if (sessionStorage.getItem(storageKey)) {
try {
return JSON.parse(sessionStorage.getItem(storageKey));
} catch (ignored) {
// catch any errors here so it does not stop script execution
}
}

return {};
}

// if we have auth in storage use it
try {
const currentAuthorizations = getAuthorizationsFromStorage();
Object.keys(currentAuthorizations).forEach(k => ui.authActions.authorize({[k]: currentAuthorizations[k]}));
} catch (ignored) {
// catch any errors here so it does not stop script execution
}

// hook into authorize to store the auth in local storage when user performs authorization
const currentAuthorize = ui.authActions.authorize;
ui.authActions.authorize = function (payload) {
sessionStorage.setItem(storageKey, JSON.stringify(payload));
try {
sessionStorage.setItem(storageKey, JSON.stringify(Object.assign(
getAuthorizationsFromStorage(),
payload
)));
} catch (ignored) {
// catch any errors here so it does not stop script execution
}

return currentAuthorize(payload);
};

// hook into logout to clear auth from storage if user logs out
const currentLogout = ui.authActions.logout;
ui.authActions.logout = function (payload) {
sessionStorage.removeItem(storageKey);
try {
let currentAuth = getAuthorizationsFromStorage();
payload.forEach(k => delete currentAuth[k]);
sessionStorage.setItem(storageKey, JSON.stringify(currentAuth));
} catch (ignored) {
// catch any errors here so it does not stop script execution
}

return currentLogout(payload);
};

Expand Down

0 comments on commit be67a3a

Please sign in to comment.