56 lines
No EOL
1.7 KiB
TypeScript
56 lines
No EOL
1.7 KiB
TypeScript
import axios from "axios"
|
|
import { apiUrl } from "./api"
|
|
|
|
export interface Session {
|
|
sessionToken: string;
|
|
expiresOn: string;
|
|
}
|
|
|
|
export interface Login {
|
|
username: string;
|
|
isAdmin: string;
|
|
}
|
|
|
|
export const logIn = async (username: string, password: string) => {
|
|
const response = await axios.post<Session>(`${apiUrl}/auth/login`, `username=${encodeURI(username)}&password=${encodeURI(password)}`);
|
|
|
|
return response.data;
|
|
}
|
|
|
|
export const GetLoginFromSessonCookie = async () => {
|
|
const response = await axios.get<Login>(`${apiUrl}/auth`, { withCredentials: true });
|
|
|
|
return response.data;
|
|
}
|
|
|
|
export const IsCurrentUserAdmin = async () => {
|
|
return (await GetLoginFromSessonCookie()).isAdmin;
|
|
}
|
|
|
|
export const AddUser = async (username: string, password: string, isAdmin: boolean) => {
|
|
const response = await axios.post<Login>(`${apiUrl}/auth`, { username: username, password: password, isAdmin: isAdmin }, { withCredentials: true });
|
|
|
|
return response.data;
|
|
}
|
|
|
|
export const DeleteUser = async (username: string) => {
|
|
const response = await axios.delete<Login>(`${apiUrl}/auth`, { data: { username: username }, withCredentials: true });
|
|
|
|
return response.data;
|
|
}
|
|
|
|
export const GetUserList = async () => {
|
|
const response = await axios.get<Array<Login>>(`${apiUrl}/auth/all`, { withCredentials: true });
|
|
|
|
return response.data;
|
|
}
|
|
|
|
export const GetCurrentUser = async () => {
|
|
const resonse = await axios.get<Login>(`${apiUrl}/auth`, { withCredentials: true });
|
|
|
|
return resonse.data;
|
|
}
|
|
|
|
export const ChangeCurrentUserPassword = async (oldPassword: string, newPassword: string) => {
|
|
await axios.put(`${apiUrl}/auth`, { password: oldPassword, newPassword: newPassword }, { withCredentials: true });
|
|
} |