-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert ":bug: Fix session expiry when refresh tokens are disabled (#1614
)" This reverts commit eea7835.
- Loading branch information
Showing
3 changed files
with
91 additions
and
51 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,14 @@ | ||
import axios from "axios"; | ||
import keycloak from "@app/keycloak"; | ||
|
||
export const initInterceptors = () => { | ||
export const initInterceptors = (getToken: () => Promise<string>) => { | ||
axios.interceptors.request.use( | ||
(config) => { | ||
const token = keycloak.token; | ||
if (token) { | ||
config.headers.Authorization = `Bearer ${token}`; | ||
} | ||
async (config) => { | ||
const token = await getToken(); | ||
if (token) config.headers["Authorization"] = "Bearer " + token; | ||
return config; | ||
}, | ||
(error) => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
axios.interceptors.response.use( | ||
(response) => { | ||
return response; | ||
}, | ||
async (error) => { | ||
if (error.response && error.response.status === 401) { | ||
try { | ||
const refreshed = await keycloak.updateToken(5); | ||
if (refreshed) { | ||
const retryConfig = { | ||
...error.config, | ||
headers: { | ||
...error.config.headers, | ||
Authorization: `Bearer ${keycloak.token}`, | ||
}, | ||
}; | ||
return axios(retryConfig); | ||
} | ||
} catch (refreshError) { | ||
keycloak.login(); | ||
} | ||
} | ||
return Promise.reject(error); | ||
Promise.reject(error); | ||
} | ||
); | ||
}; |
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
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