Merge branch 'dev' into feat/leerpad-vragen

This commit is contained in:
Timo De Meyst 2025-04-23 20:47:16 +02:00
commit 90ece281fd
20 changed files with 204 additions and 37 deletions

View file

@ -3,6 +3,9 @@
import MenuBar from "@/components/MenuBar.vue";
import { useRoute } from "vue-router";
import { computed } from "vue";
import authService from "@/services/auth/auth-service.ts";
void authService.loadUser();
const route = useRoute();
interface RouteMeta {

View file

@ -11,7 +11,7 @@
const { t, locale } = useI18n();
const role = auth.authState.activeRole;
const router = useRouter();
const _router = useRouter(); // Zonder '_' gaf dit een linter error voor unused variable
const name: string = auth.authState.user!.profile.name!;
const initials: string = name

View file

@ -1,8 +1,24 @@
export const apiConfig = {
baseUrl:
window.location.hostname === "localhost" && !(window.location.port === "80" || window.location.port === "")
? "http://localhost:3000/api"
: window.location.origin + "/api",
baseUrl: ((): string => {
if (import.meta.env.MODE === "test") {
// TODO Remove hardcoding
return "http://localhost:9876/api";
}
if (import.meta.env.VITE_API_BASE_URL) {
return import.meta.env.VITE_API_BASE_URL;
}
if (
window.location.hostname === "localhost" &&
!(window.location.port === "80" || window.location.port === "")
) {
return "http://localhost:3000/api";
}
// Fallback to the current origin with "/api" suffix
return `${window.location.origin}/api`;
})(),
};
export const loginRoute = "/login";

View file

@ -1,6 +1,7 @@
import apiClient from "@/services/api-client/api-client.ts";
import type { AxiosResponse, ResponseType } from "axios";
import { HttpErrorResponseException } from "@/exception/http-error-response-exception.ts";
import { apiConfig } from "@/config.ts";
export abstract class BaseController {
protected basePath: string;
@ -16,9 +17,18 @@ export abstract class BaseController {
}
protected async get<T>(path: string, queryParams?: QueryParams, responseType?: ResponseType): Promise<T> {
const response = await apiClient.get<T>(this.absolutePathFor(path), { params: queryParams, responseType });
BaseController.assertSuccessResponse(response);
return response.data;
try {
const response = await apiClient.get<T>(this.absolutePathFor(path), { params: queryParams, responseType });
BaseController.assertSuccessResponse(response);
return response.data;
} catch (error) {
if (error instanceof HttpErrorResponseException) {
throw error;
}
throw new Error(
`An unexpected error occurred while fetching data from ${apiConfig.baseUrl}${this.absolutePathFor(path)}: ${error}`,
);
}
}
protected async post<T>(path: string, body: unknown, queryParams?: QueryParams): Promise<T> {

View file

@ -12,11 +12,9 @@ import App from "./App.vue";
import router from "./router";
import { aliases, mdi } from "vuetify/iconsets/mdi";
import { VueQueryPlugin, QueryClient } from "@tanstack/vue-query";
import authService from "./services/auth/auth-service.ts";
const app = createApp(App);
await authService.loadUser();
app.use(router);
const link = document.createElement("link");

View file

@ -138,7 +138,7 @@ const router = createRouter({
router.beforeEach(async (to, _from, next) => {
// Verify if user is logged in before accessing certain routes
if (to.meta.requiresAuth) {
if (!authService.isLoggedIn.value) {
if (!authService.isLoggedIn.value && !(await authService.loadUser())) {
next("/login");
} else {
next();

View file

@ -37,7 +37,20 @@ async function loadUser(): Promise<User | null> {
if (!activeRole) {
return null;
}
const user = await (await getUserManagers())[activeRole].getUser();
const userManager = (await getUserManagers())[activeRole];
let user = await userManager.getUser(); // Load the user from the local storage.
if (!user) {
// If the user is not in the local storage, he could still be authenticated in Keycloak.
try {
user = await userManager.signinSilent();
} catch (_: unknown) {
// When the user was previously logged in and then logged out, signinSilent throws an error.
// In that case, the user is not authenticated anymore, so we set him to null.
user = null;
}
}
setUserAuthInfo(user);
authState.activeRole = activeRole ?? null;
return user;