Skip to content

Instantly share code, notes, and snippets.

@AliMilani
Last active October 4, 2022 05:49
Show Gist options
  • Select an option

  • Save AliMilani/baf55d4037c8580e25967fa520b264e8 to your computer and use it in GitHub Desktop.

Select an option

Save AliMilani/baf55d4037c8580e25967fa520b264e8 to your computer and use it in GitHub Desktop.
swaggerOptions: {
persistAuthorization: true,
responseInterceptor: function (response) {
if (!response.url.includes("auth/signin")) return response;
const accessToken = response.headers["x-access-token"];
console.log(response, accessToken);
if (!accessToken) return response;
let oldAuth = JSON.parse(localStorage.getItem("authorized"));
if (oldAuth && oldAuth.accessToken) {
oldAuth.accessToken.value = accessToken;
localStorage.setItem("authorized", JSON.stringify(oldAuth));
} else {
const newAuth = {
accessToken: {
name: "accessToken",
schema: {
type: "apiKey",
description:
"you can get this token from /auth/login endpoint at the response header",
name: "x-access-token",
in: "header",
},
value: `${accessToken}`,
},
};
localStorage.setItem("authorized", JSON.stringify(newAuth));
}
ui.authActions.authorize({
accessToken: {
name: "accessToken",
schema: {
type: "apiKey",
in: "header",
name: "x-access-token",
description:
"you can get this token from /auth/login endpoint at the response header",
},
value: accessToken,
},
});
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment