feat: autenticação mockada com cookie

This commit is contained in:
Junior
2026-03-14 10:34:51 -03:00
parent ccaf29f229
commit a78b9eb314
2 changed files with 45 additions and 0 deletions

View File

@@ -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<AuthUser | null>('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 }
}

View File

@@ -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('/')
}
})