import { defineStore } from 'pinia'; import { authService } from '~/services/auth.service'; interface User { id: string; email: string; firstName: string; lastName: string; role: 'INSTRUCTOR' | 'ADMIN' | 'STUDENT'; } export const useAuthStore = defineStore('auth', { state: () => ({ user: null as User | null, token: null as string | null, isAuthenticated: false }), getters: { isInstructor: (state) => state.user?.role === 'INSTRUCTOR', isAdmin: (state) => state.user?.role === 'ADMIN', isStudent: (state) => state.user?.role === 'STUDENT' }, actions: { async login(email: string, password: string) { try { const response = await authService.login(email, password); this.token = response.token; this.user = response.user as User; this.isAuthenticated = true; // Save to cookies const tokenCookie = useCookie('token', { maxAge: 60 * 60 * 24, // 24 hours sameSite: 'strict' }); const refreshTokenCookie = useCookie('refreshToken', { maxAge: 60 * 60 * 24 * 7, // 7 days sameSite: 'strict' }); const userCookie = useCookie('user', { maxAge: 60 * 60 * 24, // 24 hours sameSite: 'strict' }); tokenCookie.value = this.token; refreshTokenCookie.value = response.refreshToken; userCookie.value = JSON.stringify(this.user); return { token: this.token, user: this.user }; } catch (error: any) { throw error; } }, logout() { this.user = null; this.token = null; this.isAuthenticated = false; // Clear cookies const tokenCookie = useCookie('token'); const refreshTokenCookie = useCookie('refreshToken'); const userCookie = useCookie('user'); tokenCookie.value = null; refreshTokenCookie.value = null; userCookie.value = null; }, checkAuth() { const tokenCookie = useCookie('token'); const userCookie = useCookie('user'); if (tokenCookie.value && userCookie.value) { this.token = tokenCookie.value; try { this.user = typeof userCookie.value === 'string' ? JSON.parse(userCookie.value) : userCookie.value; this.isAuthenticated = true; } catch (e) { // Invalid user data this.logout(); } } } } });