Skip to content

Commit

Permalink
chore: code-cleanup, refactoring (immutability)
Browse files Browse the repository at this point in the history
  • Loading branch information
paras-verma committed Oct 21, 2024
1 parent 30835f9 commit 5de7ab2
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ export class OIDCClient {
this.refreshTokenLock = false;
}

async prepareHeaders(HEADERS) {
if (!HEADERS) HEADERS = this.BASE_HEADERS;
async prepareHeaders(headers) {
if (!headers) headers = this.BASE_HEADERS;

const token = await this.getAccessToken();

if (token) return { ...HEADERS, Authorization: `Bearer ${token}` };
else return HEADERS;
if (token) return { ...headers, Authorization: `Bearer ${token}` };
return headers;
}

async getAccessToken() {
Expand Down Expand Up @@ -92,11 +92,9 @@ export class OIDCClient {
}

async _refreshToken() {
const headers = { ...this.BASE_HEADERS };

const refreshToken = localStorage.getItem("refreshToken");

const token = sessionStorage.getItem("token");
const refreshToken = localStorage.getItem("refreshToken");
const headers = { ...this.BASE_HEADERS };

if (!refreshToken) throw "No refresh token";

Expand All @@ -105,31 +103,26 @@ export class OIDCClient {
if (token) headers["Authorization"] = `Bearer ${token}`;
headers["Refresh-Token"] = refreshToken;

let base = this.getBaseUrl();
const base = this.getBaseUrl();
if (!base) throw new Error("Missing `baseUrl` argument for OIDCClient");
if (!base.endsWith("/")) base = `${base}/`;

let refreshPath = this.getRefreshPath();
if (refreshPath.startsWith("/")) refreshPath = refreshPath.slice(1);

const serviceUrl = `${base}${refreshPath}`;
const refreshPath = this.getRefreshPath();
const serviceUrl = base.replace(/\/?$/, "/").concat(refreshPath.replace(/^\/?/, ""));

return fetch(serviceUrl, { method: "GET", headers: headers })
.then(async (response) => {
if (response.status === 403) {
throw 403;
} else {
const data = await response.json();
const token = data?.["token"] || response.headers.get("token");
const refreshToken = data?.["refreshToken"] || response.headers.get("refreshToken");

if (!token && !refreshToken) throw new Error("Couldn't fetch `access-token` or `refresh-token`");
if (token) sessionStorage.setItem("token", token);
if (refreshToken) localStorage.setItem("refreshToken", refreshToken);
}
if (response.status === 403) throw 403;

const data = await response.json();
const token = data?.["token"] || response.headers.get("token");
const refreshToken = data?.["refreshToken"] || response.headers.get("refreshToken");

if (!token && !refreshToken) throw new Error("Couldn't fetch `access-token` or `refresh-token`");
if (token) sessionStorage.setItem("token", token);
if (refreshToken) localStorage.setItem("refreshToken", refreshToken);
})
.catch((err) => {
console.log(err);
console.log("Failed to refresh tokens", err);
throw err;
})
.finally(() => {
Expand Down

0 comments on commit 5de7ab2

Please sign in to comment.