chad/client/app/composables/use-auth.ts
Никита Круглицкий ec67be8aa6
All checks were successful
Deploy / deploy (push) Successful in 4m32s
куча говна
2025-10-20 00:10:13 +06:00

70 lines
1.2 KiB
TypeScript

import chadApi from '#shared/chad-api'
import { createGlobalState } from '@vueuse/core'
interface Me {
id: string
username: string
displayName: string
}
export const useAuth = createGlobalState(() => {
const me = shallowRef<Me>()
function setMe(value: Me | undefined): void {
me.value = value
}
async function login(username: string, password: string): Promise<void> {
try {
const result = await chadApi('/login', {
method: 'POST',
body: {
username,
password,
},
})
setMe(result)
await navigateTo('/')
}
catch {}
}
async function register(username: string, password: string): Promise<void> {
try {
const result = await chadApi('/register', {
method: 'POST',
body: {
username,
password,
},
})
setMe(result)
await navigateTo('/')
}
catch {}
}
async function logout(): Promise<void> {
try {
await chadApi('/logout', { method: 'POST' })
setMe(undefined)
await navigateTo({ name: 'Login' })
}
catch {}
}
return {
me: readonly(me),
setMe,
login,
register,
logout,
}
})