client/src/services/authentication.ts
2024-06-06 19:04:22 +02:00

29 lines
1.0 KiB
TypeScript

import axios from 'axios'
import config from '@/config'
import { useSocketStore } from '@/stores/socket'
import { useCookies } from '@vueuse/integrations/useCookies'
export async function register(username: string, password: string, socketStore = useSocketStore()) {
try {
const response = await axios.post(`${config.server_endpoint}/register`, { username, password })
useCookies().set('token', response.data.token as string)
await socketStore.setupSocketConnection()
return { success: true }
} catch (error: any) {
return { error: error.response.data.message }
}
}
export async function login(username: string, password: string, socketStore = useSocketStore()) {
const response = await axios.post(`${config.server_endpoint}/login`, { username, password })
console.log(response.status, response.data);
if (response.status !== 200) {
return { error: response.data.message }
}
useCookies().set('token', response.data.token as string)
await socketStore.setupSocketConnection()
return { success: true }
}