87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
|
|
import axios from "axios";
|
||
|
|
import { API_ENDPOINTS } from "@/config/api";
|
||
|
|
import type { LoginResquest, LoginResponse } from "@/types/auth";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Đăng nhập
|
||
|
|
* @param credentials - Thông tin đăng nhập
|
||
|
|
* @returns Response chứa token, name, username, access, role
|
||
|
|
*/
|
||
|
|
export async function login(credentials: LoginResquest): Promise<LoginResponse> {
|
||
|
|
const response = await axios.post<LoginResponse>(
|
||
|
|
API_ENDPOINTS.AUTH.LOGIN,
|
||
|
|
credentials
|
||
|
|
);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Đăng xuất
|
||
|
|
*/
|
||
|
|
export async function logout(): Promise<void> {
|
||
|
|
await axios.delete(API_ENDPOINTS.AUTH.LOGOUT);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Kiểm tra phiên đăng nhập
|
||
|
|
* @param token - Access token
|
||
|
|
* @returns Response kiểm tra phiên
|
||
|
|
*/
|
||
|
|
export async function ping(token?: string): Promise<{ message: string; code: number }> {
|
||
|
|
const response = await axios.get(API_ENDPOINTS.AUTH.PING, {
|
||
|
|
params: token ? { token } : undefined,
|
||
|
|
});
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Lấy CSRF token
|
||
|
|
* @returns CSRF token
|
||
|
|
*/
|
||
|
|
export async function getCsrfToken(): Promise<{ token: string }> {
|
||
|
|
const response = await axios.get(API_ENDPOINTS.AUTH.CSRF_TOKEN);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Thay đổi mật khẩu của user hiện tại
|
||
|
|
* @param data - Dữ liệu thay đổi mật khẩu {currentPassword, newPassword}
|
||
|
|
*/
|
||
|
|
export async function changePassword(data: {
|
||
|
|
currentPassword: string;
|
||
|
|
newPassword: string;
|
||
|
|
}): Promise<{ message: string }> {
|
||
|
|
const response = await axios.put(API_ENDPOINTS.AUTH.CHANGE_PASSWORD, data);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Admin thay đổi mật khẩu của user khác
|
||
|
|
* @param data - Dữ liệu {username, newPassword}
|
||
|
|
*/
|
||
|
|
export async function changePasswordAdmin(data: {
|
||
|
|
username: string;
|
||
|
|
newPassword: string;
|
||
|
|
}): Promise<{ message: string }> {
|
||
|
|
const response = await axios.put(
|
||
|
|
API_ENDPOINTS.AUTH.CHANGE_PASSWORD_ADMIN,
|
||
|
|
data
|
||
|
|
);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Tạo tài khoản mới
|
||
|
|
* @param data - Dữ liệu tạo tài khoản
|
||
|
|
*/
|
||
|
|
export async function createAccount(data: {
|
||
|
|
userName: string;
|
||
|
|
password: string;
|
||
|
|
name: string;
|
||
|
|
roleId: number;
|
||
|
|
accessBuildings?: number[];
|
||
|
|
}): Promise<{ message: string }> {
|
||
|
|
const response = await axios.post(API_ENDPOINTS.AUTH.CREATE_ACCOUNT, data);
|
||
|
|
return response.data;
|
||
|
|
}
|