-
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.
🐛 Fix session expiry when refresh tokens are disabled
Signed-off-by: ibolton336 <[email protected]>
- Loading branch information
1 parent
e99029b
commit 9811757
Showing
3 changed files
with
51 additions
and
91 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,14 +1,43 @@ | ||
import axios from "axios"; | ||
import keycloak from "@app/keycloak"; | ||
|
||
export const initInterceptors = (getToken: () => Promise<string>) => { | ||
export const initInterceptors = () => { | ||
axios.interceptors.request.use( | ||
async (config) => { | ||
const token = await getToken(); | ||
if (token) config.headers["Authorization"] = "Bearer " + token; | ||
(config) => { | ||
const token = keycloak.token; | ||
if (token) { | ||
config.headers.Authorization = `Bearer ${token}`; | ||
} | ||
return config; | ||
}, | ||
(error) => { | ||
Promise.reject(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); | ||
} | ||
); | ||
}; |
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