diff --git a/frontend/composables/user.ts b/frontend/composables/user.ts index 92cccb0..1d81868 100644 --- a/frontend/composables/user.ts +++ b/frontend/composables/user.ts @@ -1,41 +1,46 @@ import { z } from 'zod' const userSchema = z.object({ - email: z.string().email(), - password: z.string().min(6), + email: z.string().email(), + password: z.string().min(6), +}) + +const loggedInUserSchema = z.object({ + name: z.string(), }) const userLogin = async (email: String, password: String) => { - await getUser() - if (useUser().value) return + await getUser() + if (useUser().value) return - const user = userSchema.safeParse({ email, password }) + const user = userSchema.safeParse({ email, password }) - if (!user.success) return user.error.format() + if (!user.success) return user.error.format() - await request.get('/sanctum/csrf-cookie') - await request.post('/login', user.data) + await request.get('/sanctum/csrf-cookie') + await request.post('/login', user.data) - await getUser() + await getUser() } const getUser = async () => { - await request - .get('/api/user') - .then(({ data }) => { - const parsed = loggedInUserSchema.safeParse(data) - if (!parsed.success) return - useUser().value = parsed.data.name - }) - .catch((error) => { - console.log(error) - }) + await request + .get('/api/user') + .then(({ data }) => { + const parsed = loggedInUserSchema.safeParse(data) + if (!parsed.success) return + useUser().value = parsed.data.name + }) + .catch((error) => { + console.log(error) + }) } -const loggedInUserSchema = z.object({ - name: z.string(), -}) +const userLogout = async () => { + await request.post('/logout') + useUser().value = '' +} const useUser = () => useState('user', () => '') -export { userLogin, useUser, getUser } +export { userLogin, useUser, getUser, userLogout } diff --git a/frontend/pages/login/index.vue b/frontend/pages/login/index.vue index 566c2c8..5cd7688 100644 --- a/frontend/pages/login/index.vue +++ b/frontend/pages/login/index.vue @@ -1,89 +1,69 @@