feat: autenticação mockada com cookie
This commit is contained in:
36
front-end/app/composables/useAuth.ts
Normal file
36
front-end/app/composables/useAuth.ts
Normal 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 }
|
||||||
|
}
|
||||||
9
front-end/app/middleware/auth.global.ts
Normal file
9
front-end/app/middleware/auth.global.ts
Normal 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('/')
|
||||||
|
}
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user