2026-02-24 00:20:54 -06:00
|
|
|
export const useAuth = () => {
|
|
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
|
|
|
|
|
const login = async (email: string, password: string) => {
|
|
|
|
|
await $fetch('/sanctum/csrf-cookie', {
|
|
|
|
|
baseURL: config.public.apiBase,
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
})
|
|
|
|
|
await $api('/api/login', {method: 'POST', body: {email, password}})
|
2026-04-08 22:37:38 -05:00
|
|
|
window.location.href = '/lists'
|
2026-02-24 00:20:54 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 00:36:20 -05:00
|
|
|
const register = async (email: string, username: string) => {
|
|
|
|
|
await $fetch('/sanctum/csrf-cookie', {
|
|
|
|
|
baseURL: config.public.apiBase,
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
})
|
|
|
|
|
await $api('/api/register', {method: 'POST', body: {email, username}})
|
2026-02-25 17:33:37 -06:00
|
|
|
await navigateTo('/auth/login')
|
2026-02-24 00:20:54 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-05 00:36:20 -05:00
|
|
|
const resetPassword = async (password: string, passwordConfirmation: string, token: string, email: string) => {
|
|
|
|
|
await $fetch('/sanctum/csrf-cookie', {
|
|
|
|
|
baseURL: config.public.apiBase,
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
})
|
|
|
|
|
await $api('/api/reset-password', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: {
|
|
|
|
|
password,
|
|
|
|
|
password_confirmation: passwordConfirmation,
|
|
|
|
|
token,
|
|
|
|
|
email
|
|
|
|
|
},
|
|
|
|
|
onResponseError: (context) => {
|
|
|
|
|
if (context.response.status === 401) {
|
|
|
|
|
throw new Error('TOKEN_EXPIRED')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
await navigateTo('/lists')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const xsrfToken = useCookie('XSRF-TOKEN')
|
|
|
|
|
|
|
|
|
|
const logout = async () => {
|
|
|
|
|
await $fetch('/api/logout', {
|
|
|
|
|
baseURL: config.public.apiBase,
|
|
|
|
|
method: 'POST',
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
headers: {
|
|
|
|
|
...(xsrfToken.value ? {'X-XSRF-TOKEN': xsrfToken.value} : {}),
|
|
|
|
|
},
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
alert("Failed to logout. Please try again.")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
useCookie('XSRF-TOKEN').value = ''
|
|
|
|
|
navigateTo('/auth/login')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {login, register, resetPassword, logout}
|
2026-02-24 00:20:54 -06:00
|
|
|
}
|