41 lines
816 B
TypeScript
41 lines
816 B
TypeScript
import type { User } from '~/types'
|
|
|
|
export const useAuth = () => {
|
|
const authStore = useAuthStore()
|
|
|
|
const isAuthenticated = computed(() => authStore.isAuthenticated)
|
|
const user = computed<User | null>(() => authStore.user)
|
|
const loading = computed(() => authStore.loading)
|
|
|
|
const login = async (email: string, password: string) => {
|
|
await authStore.login(email, password)
|
|
}
|
|
|
|
const register = async (data: {
|
|
name: string
|
|
email: string
|
|
password: string
|
|
password_confirm: string
|
|
phone?: string
|
|
}) => {
|
|
await authStore.register(data)
|
|
}
|
|
|
|
const logout = () => {
|
|
authStore.logout()
|
|
navigateTo('/')
|
|
}
|
|
|
|
const fetchUser = () => authStore.fetchUser()
|
|
|
|
return {
|
|
isAuthenticated,
|
|
user,
|
|
loading,
|
|
login,
|
|
register,
|
|
logout,
|
|
fetchUser,
|
|
}
|
|
}
|