60 lines
1.5 KiB
TypeScript
60 lines
1.5 KiB
TypeScript
import { AxiosError } from "axios";
|
|
import { LOGIN_URI, SIGNUP_URI } from "../constants/api";
|
|
import { client } from "./config";
|
|
import { IHttpResponse } from "../types/common";
|
|
|
|
const initialState: IHttpResponse = {
|
|
data: null,
|
|
error: AxiosError
|
|
}
|
|
|
|
interface IAuthentication {
|
|
username: String
|
|
password: String
|
|
}
|
|
|
|
async function createAccountService({ username, password }: IAuthentication) {
|
|
const newState = { ...initialState };
|
|
try {
|
|
const response = await client({ method: 'POST', url: SIGNUP_URI, data: { username, password }, withCredentials: true })
|
|
newState.data = response.data
|
|
newState.error = null
|
|
return newState
|
|
} catch (error) {
|
|
newState.error = error
|
|
return newState
|
|
}
|
|
}
|
|
|
|
async function loginService({ username, password }: IAuthentication) {
|
|
const newState = { ...initialState };
|
|
try {
|
|
const response = await client({ method: 'POST', url: LOGIN_URI, data: { username, password }, withCredentials: true })
|
|
newState.data = response.data
|
|
newState.error = null
|
|
return newState
|
|
} catch (error) {
|
|
newState.error = error
|
|
return newState
|
|
}
|
|
|
|
}
|
|
|
|
async function logoutService() {
|
|
const newState = { ...initialState };
|
|
try {
|
|
const response = await client({ method: 'POST', url: LOGIN_URI})
|
|
newState.data = response.data
|
|
newState.error = null
|
|
return newState
|
|
} catch (error) {
|
|
newState.error = error
|
|
return newState
|
|
}
|
|
}
|
|
|
|
export {
|
|
loginService,
|
|
createAccountService,
|
|
logoutService
|
|
}; |