Update frontend composables (format)

main
TZGyn 2 years ago
parent fa1645899a
commit fadc2ee071
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -3,25 +3,25 @@ import type { UseFetchOptions } from 'nuxt/app'
const config = useRuntimeConfig()
const useCustomFetch = async <T>(
path: string,
options: UseFetchOptions<T> = {}
path: string,
options: UseFetchOptions<T> = {}
) => {
let headers: any = {}
const token = useCookie('XSRF-TOKEN')
let headers: any = {}
const token = useCookie('XSRF-TOKEN')
if (token.value) {
headers['X-XSRF-TOKEN'] = token.value as string
}
if (token.value) {
headers['X-XSRF-TOKEN'] = token.value as string
}
return useFetch(config.public.laravelApi + path, {
credentials: 'include',
watch: false,
...options,
headers: {
...headers,
...options?.headers,
},
})
return useFetch(config.public.laravelApi + path, {
credentials: 'include',
watch: false,
...options,
headers: {
...headers,
...options?.headers,
},
})
}
export { useCustomFetch }

@ -1,58 +1,58 @@
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(),
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 useCustomFetch('/sanctum/csrf-cookie')
await useCustomFetch('/login', { method: 'POST', body: user.data })
await useCustomFetch('/sanctum/csrf-cookie')
await useCustomFetch('/login', { method: 'POST', body: user.data })
await getUser()
await getUser()
}
const newUserSchema = z.object({
name: z.string(),
email: z.string().email(),
password: z.string().min(6),
password_confirmation: z.string().min(6),
name: z.string(),
email: z.string().email(),
password: z.string().min(6),
password_confirmation: z.string().min(6),
})
const userSignup = async (data: unknown) => {
const newUser = newUserSchema.safeParse(data)
const newUser = newUserSchema.safeParse(data)
if (!newUser.success) return newUser.error.format()
if (!newUser.success) return newUser.error.format()
await useCustomFetch('/register', { method: 'POST', body: newUser.data })
await useCustomFetch('/register', { method: 'POST', body: newUser.data })
await getUser()
await getUser()
}
const getUser = async () => {
const { data, error } = await useCustomFetch('/api/user')
const { data, error } = await useCustomFetch('/api/user')
if (error.value) return
if (error.value) return
const parsed = loggedInUserSchema.safeParse(data.value)
if (!parsed.success) return
useUser().value = parsed.data.name
const parsed = loggedInUserSchema.safeParse(data.value)
if (!parsed.success) return
useUser().value = parsed.data.name
}
const userLogout = async () => {
await useCustomFetch('/logout', { method: 'POST' })
useUser().value = ''
await useCustomFetch('/logout', { method: 'POST' })
useUser().value = ''
}
const useUser = () => useState<String>('user', () => '')

@ -1,5 +1,5 @@
const openUrl = (url: string) => {
window.open(url, '_blank')
window.open(url, '_blank')
}
export { openUrl }

Loading…
Cancel
Save