70 lines
1.5 KiB
TypeScript
70 lines
1.5 KiB
TypeScript
export interface User {
|
|
id: string
|
|
email: string
|
|
}
|
|
|
|
export default () => {
|
|
const nuxtApp = useNuxtApp()
|
|
const user = useState<User | null>('user')
|
|
|
|
const authenticated = computed(() => !!user.value)
|
|
|
|
async function getUser() {
|
|
user.value = await $api('/users/current', { method: 'GET' })
|
|
}
|
|
|
|
async function login(email: string, password: string) {
|
|
await $api('/sessions', { method: 'POST', body: { email, password } })
|
|
await getUser()
|
|
|
|
navigateTo('/projects')
|
|
}
|
|
|
|
async function register(email: string, password: string) {
|
|
await $api('/users', { method: 'POST', body: { email, password } })
|
|
|
|
navigateTo('/login')
|
|
}
|
|
|
|
async function logout() {
|
|
try {
|
|
await $api('/sessions', { method: 'delete', body: {} })
|
|
}
|
|
finally {
|
|
clearNuxtState('user')
|
|
navigateTo('/login')
|
|
}
|
|
}
|
|
|
|
async function requestResetPassword(email: string) {
|
|
await $api('/users/password_reset', { method: 'post', body: { email } })
|
|
}
|
|
|
|
async function resetPassword(newPassword: string, resetCode: string) {
|
|
await $api('/users/password_reset', {
|
|
method: 'put',
|
|
body: {
|
|
newPassword,
|
|
resetCode,
|
|
},
|
|
})
|
|
}
|
|
async function resendVerificationCode(email: string) {
|
|
await $api('/users/verification', {
|
|
method: 'put',
|
|
body: { email },
|
|
})
|
|
}
|
|
|
|
return {
|
|
user,
|
|
authenticated,
|
|
login,
|
|
register,
|
|
logout,
|
|
resendVerificationCode,
|
|
requestResetPassword,
|
|
resetPassword,
|
|
}
|
|
}
|