diff --git a/app/app.vue b/app/app.vue
index 732c82f..32b561b 100644
--- a/app/app.vue
+++ b/app/app.vue
@@ -5,10 +5,13 @@
\ No newline at end of file
diff --git a/app/composables/useAuth.ts b/app/composables/useAuth.ts
index fefc069..6c2073f 100644
--- a/app/composables/useAuth.ts
+++ b/app/composables/useAuth.ts
@@ -4,6 +4,13 @@ interface User {
id: string;
email: string;
nickname: string;
+ avatar: string | null;
+ coins: number;
+ exp: number;
+ soundOn: boolean;
+ confettiOn: boolean;
+ createdAt: string;
+ updatedAt: string;
}
export function useAuth() {
@@ -17,16 +24,20 @@ export function useAuth() {
const isAuthenticated = computed(() => !!user.value);
const fetchMe = async () => {
+ // This function can be called multiple times, but the logic inside
+ // will only run once thanks to the initialized flag.
if (initialized.value) return;
loading.value = true;
- initialized.value = true;
try {
- user.value = await api('/auth/me', { method: 'GET' });
+ // The backend returns the user object nested under a 'user' key.
+ const response = await api<{ user: User }>('/auth/me', { method: 'GET' });
+ user.value = response.user; // Correctly assign the nested user object
} catch (error) {
- user.value = null; // Silently set user to null
+ user.value = null; // Silently set user to null on 401
} finally {
loading.value = false;
+ initialized.value = true; // Mark as initialized after the first attempt
}
};
diff --git a/app/layouts/default.vue b/app/layouts/default.vue
index 083272c..c38e2ec 100644
--- a/app/layouts/default.vue
+++ b/app/layouts/default.vue
@@ -1,13 +1,13 @@
-
+
+
diff --git a/middleware/auth.global.ts b/middleware/auth.global.ts
index ce21759..dd73011 100644
--- a/middleware/auth.global.ts
+++ b/middleware/auth.global.ts
@@ -1,16 +1,19 @@
// /middleware/auth.ts
export default defineNuxtRouteMiddleware((to) => {
- // `app.vue` is responsible for the initial fetchUser call.
- // This middleware just reads the state that's already present.
- const { isAuthenticated } = useAuth();
+ const { isAuthenticated, initialized } = useAuth();
+ // Do not run middleware until auth state is initialized on client-side
+ if (!initialized.value) {
+ return;
+ }
+
// if the user is authenticated and tries to access /login, redirect to home
if (isAuthenticated.value && to.path === '/login') {
return navigateTo('/', { replace: true });
}
// if the user is not authenticated and tries to access any page other than public routes, redirect to /login
- const publicRoutes = ['/login', '/register']; // Add any other public paths here
+ const publicRoutes = ['/login', '/register'];
if (!isAuthenticated.value && !publicRoutes.includes(to.path)) {
return navigateTo('/login', { replace: true });
}