fix(frontend): Fixed infinite retry loop when unauthorized
Also adjusted the Keycloak config
This commit is contained in:
		
							parent
							
								
									d64b4505c8
								
							
						
					
					
						commit
						0b2c90c8b5
					
				
					 4 changed files with 17 additions and 14 deletions
				
			
		|  | @ -7,5 +7,4 @@ const apiClient = axios.create({ | ||||||
|         "Content-Type": "application/json", |         "Content-Type": "application/json", | ||||||
|     }, |     }, | ||||||
| }); | }); | ||||||
| 
 |  | ||||||
| export default apiClient; | export default apiClient; | ||||||
|  |  | ||||||
|  | @ -7,12 +7,12 @@ import type {AuthState, Role, UserManagersForRoles} from "@/services/auth/auth-t | ||||||
| import {User, UserManager} from "oidc-client-ts"; | import {User, UserManager} from "oidc-client-ts"; | ||||||
| import {loadAuthConfig} from "@/services/auth/auth-config-loader.ts"; | import {loadAuthConfig} from "@/services/auth/auth-config-loader.ts"; | ||||||
| import authStorage from "./auth-storage.ts" | import authStorage from "./auth-storage.ts" | ||||||
| import {useRouter} from "vue-router"; |  | ||||||
| import {loginRoute} from "@/config.ts"; | import {loginRoute} from "@/config.ts"; | ||||||
| import apiClient from "@/services/api-client.ts"; | import apiClient from "@/services/api-client.ts"; | ||||||
|  | import router from "@/router"; | ||||||
|  | import type {AxiosError} from "axios"; | ||||||
| 
 | 
 | ||||||
| const authConfig = await loadAuthConfig(); | const authConfig = await loadAuthConfig(); | ||||||
| const router = useRouter(); |  | ||||||
| 
 | 
 | ||||||
| const userManagers: UserManagersForRoles = { | const userManagers: UserManagersForRoles = { | ||||||
|     student: new UserManager(authConfig.student), |     student: new UserManager(authConfig.student), | ||||||
|  | @ -115,11 +115,15 @@ apiClient.interceptors.request.use(async (reqConfig) => { | ||||||
| // Registering interceptor to refresh the token when a request failed because it was expired.
 | // Registering interceptor to refresh the token when a request failed because it was expired.
 | ||||||
| apiClient.interceptors.response.use( | apiClient.interceptors.response.use( | ||||||
|     response => response, |     response => response, | ||||||
|     async (error) => { |     async (error: AxiosError<{message?: string}>) => { | ||||||
|         if (error.response?.status === 401) { |         if (error.response?.status === 401) { | ||||||
|  |             if (error.response!.data.message === "token_expired") { | ||||||
|                 console.log("Access token expired, trying to refresh..."); |                 console.log("Access token expired, trying to refresh..."); | ||||||
|                 await renewToken(); |                 await renewToken(); | ||||||
|             return apiClient(error.config); // Retry the request
 |                 return apiClient(error.config!); // Retry the request
 | ||||||
|  |             } else { // Apparently, the user got a 401 because he was not logged in yet at all. Redirect him to login.
 | ||||||
|  |                 await initiateLogin() | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|         return Promise.reject(error); |         return Promise.reject(error); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -544,14 +544,14 @@ | ||||||
|     "clientId" : "dwengo", |     "clientId" : "dwengo", | ||||||
|     "name" : "Dwengo", |     "name" : "Dwengo", | ||||||
|     "description" : "", |     "description" : "", | ||||||
|     "rootUrl" : "http://localhost:3000", |     "rootUrl" : "http://localhost:5173", | ||||||
|     "adminUrl" : "http://localhost:3000", |     "adminUrl" : "http://localhost:5173", | ||||||
|     "baseUrl" : "/", |     "baseUrl" : "/", | ||||||
|     "surrogateAuthRequired" : false, |     "surrogateAuthRequired" : false, | ||||||
|     "enabled" : true, |     "enabled" : true, | ||||||
|     "alwaysDisplayInConsole" : false, |     "alwaysDisplayInConsole" : false, | ||||||
|     "clientAuthenticatorType" : "client-jwt", |     "clientAuthenticatorType" : "client-jwt", | ||||||
|     "redirectUris" : [ "urn:ietf:wg:oauth:2.0:oob", "http://localhost:3000/*", "http://localhost:3000" ], |     "redirectUris" : [ "urn:ietf:wg:oauth:2.0:oob", "http://localhost:5173/*", "http://localhost:5173" ], | ||||||
|     "webOrigins" : [ "+" ], |     "webOrigins" : [ "+" ], | ||||||
|     "notBefore" : 0, |     "notBefore" : 0, | ||||||
|     "bearerOnly" : false, |     "bearerOnly" : false, | ||||||
|  |  | ||||||
|  | @ -544,14 +544,14 @@ | ||||||
|     "clientId" : "dwengo", |     "clientId" : "dwengo", | ||||||
|     "name" : "Dwengo", |     "name" : "Dwengo", | ||||||
|     "description" : "", |     "description" : "", | ||||||
|     "rootUrl" : "http://localhost:3000", |     "rootUrl" : "http://localhost:5173", | ||||||
|     "adminUrl" : "http://localhost:3000", |     "adminUrl" : "http://localhost:5173", | ||||||
|     "baseUrl" : "http://localhost:3000", |     "baseUrl" : "http://localhost:5173", | ||||||
|     "surrogateAuthRequired" : false, |     "surrogateAuthRequired" : false, | ||||||
|     "enabled" : true, |     "enabled" : true, | ||||||
|     "alwaysDisplayInConsole" : false, |     "alwaysDisplayInConsole" : false, | ||||||
|     "clientAuthenticatorType" : "client-secret", |     "clientAuthenticatorType" : "client-secret", | ||||||
|     "redirectUris" : [ "urn:ietf:wg:oauth:2.0:oob", "http://localhost:3000/*", "http://localhost:3000" ], |     "redirectUris" : [ "urn:ietf:wg:oauth:2.0:oob", "http://localhost:5173/*", "http://localhost:5173" ], | ||||||
|     "webOrigins" : [ "+" ], |     "webOrigins" : [ "+" ], | ||||||
|     "notBefore" : 0, |     "notBefore" : 0, | ||||||
|     "bearerOnly" : false, |     "bearerOnly" : false, | ||||||
|  |  | ||||||
		Reference in a new issue
	
	 Gerald Schmittinger
						Gerald Schmittinger