58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import config from '@/application/config'
|
|
import { useCookies } from '@vueuse/integrations/useCookies'
|
|
import axios from 'axios'
|
|
|
|
export async function register(username: string, email: string, password: string) {
|
|
try {
|
|
const response = await axios.post(`${config.server_endpoint}/register`, { username, email, password })
|
|
if (response.status === 200) {
|
|
return { success: true }
|
|
}
|
|
return { error: response.data.message }
|
|
} catch (error: any) {
|
|
if (typeof error.response?.data === 'undefined') {
|
|
return { error: 'Could not connect to server' }
|
|
}
|
|
return { error: error.response.data.message }
|
|
}
|
|
}
|
|
|
|
export async function login(username: string, password: string) {
|
|
try {
|
|
const response = await axios.post(`${config.server_endpoint}/login`, { username, password })
|
|
useCookies().set('token', response.data.data.token as string, {
|
|
domain: config.domain
|
|
})
|
|
return { success: true, token: response.data.data.token }
|
|
} catch (error: any) {
|
|
if (typeof error.response?.data === 'undefined') {
|
|
return { error: 'Could not connect to server' }
|
|
}
|
|
return { error: error.response.data.message }
|
|
}
|
|
}
|
|
|
|
export async function resetPassword(email: string) {
|
|
try {
|
|
const response = await axios.post(`${config.server_endpoint}/reset-password`, { email })
|
|
return { success: true, token: response.data.data.token }
|
|
} catch (error: any) {
|
|
if (typeof error.response?.data === 'undefined') {
|
|
return { error: 'Could not connect to server' }
|
|
}
|
|
return { error: error.response.data.message }
|
|
}
|
|
}
|
|
|
|
export async function newPassword(urlToken: string, password: string) {
|
|
try {
|
|
const response = await axios.post(`${config.server_endpoint}/new-password`, { urlToken, password })
|
|
return { success: true, token: response.data.data.token }
|
|
} catch (error: any) {
|
|
if (typeof error.response?.data === 'undefined') {
|
|
return { error: 'Could not connect to server' }
|
|
}
|
|
return { error: error.response.data.message }
|
|
}
|
|
}
|