1
0
forked from noxious/client
noxious_client/src/services/authentication.ts
2024-06-02 02:35:42 +02:00

33 lines
1.1 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 })
if (response.status === 200) {
useCookies().set('token', response.data.token as string)
await socketStore.setupSocketConnection()
return true
}
} catch (error) {
console.error('Error registering user:', error)
return false
}
}
export async function login(username: string, password: string, socketStore = useSocketStore()) {
try {
const response = await axios.post(`${config.server_endpoint}/login`, { username, password })
if (response.status === 200) {
useCookies().set('token', response.data.token as string)
await socketStore.setupSocketConnection()
return true
}
} catch (error) {
console.error('Login failed:', error)
return false
}
}