initial
This commit is contained in:
69
apps/client/composables/use-auth.ts
Normal file
69
apps/client/composables/use-auth.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user