From a78b9eb3144d7539a4d089afef86e1a666040b63 Mon Sep 17 00:00:00 2001 From: Junior Date: Sat, 14 Mar 2026 10:34:51 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20autentica=C3=A7=C3=A3o=20mockada=20com?= =?UTF-8?q?=20cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front-end/app/composables/useAuth.ts | 36 +++++++++++++++++++++++++ front-end/app/middleware/auth.global.ts | 9 +++++++ 2 files changed, 45 insertions(+) create mode 100644 front-end/app/composables/useAuth.ts create mode 100644 front-end/app/middleware/auth.global.ts diff --git a/front-end/app/composables/useAuth.ts b/front-end/app/composables/useAuth.ts new file mode 100644 index 0000000..574d08b --- /dev/null +++ b/front-end/app/composables/useAuth.ts @@ -0,0 +1,36 @@ +const MOCK_EMAIL = 'admin@licitatche.gov.br' +const MOCK_PASSWORD = 'admin123' + +interface AuthUser { + nome: string + email: string + papel: string +} + +export function useAuth() { + const user = useState('auth_user', () => null) + const token = useCookie('auth_token', { maxAge: 60 * 60 * 8 }) + + const isAuthenticated = computed(() => !!token.value) + + if (token.value && !user.value) { + user.value = { nome: 'Admin', email: MOCK_EMAIL, papel: 'Administrador' } + } + + async function login(email: string, password: string): Promise<{ success: boolean; error?: string }> { + if (email === MOCK_EMAIL && password === MOCK_PASSWORD) { + token.value = 'mock_token_' + Date.now() + user.value = { nome: 'Admin', email, papel: 'Administrador' } + return { success: true } + } + return { success: false, error: 'E-mail ou senha incorretos.' } + } + + function logout() { + token.value = null + user.value = null + navigateTo('/login') + } + + return { user, isAuthenticated, login, logout } +} diff --git a/front-end/app/middleware/auth.global.ts b/front-end/app/middleware/auth.global.ts new file mode 100644 index 0000000..25fbf2b --- /dev/null +++ b/front-end/app/middleware/auth.global.ts @@ -0,0 +1,9 @@ +export default defineNuxtRouteMiddleware((to) => { + const { isAuthenticated } = useAuth() + if (!isAuthenticated.value && to.path !== '/login') { + return navigateTo('/login') + } + if (isAuthenticated.value && to.path === '/login') { + return navigateTo('/') + } +})