2025-01-21 20:33:05 +03:00
|
|
|
import { queryOptions } from '@tanstack/react-query';
|
|
|
|
|
2025-01-23 19:41:31 +03:00
|
|
|
import { axiosInstance } from '@/backend/axiosInstance';
|
|
|
|
import { DELAYS } from '@/backend/configuration';
|
|
|
|
import { ICurrentUser } from '@/models/user';
|
2025-01-21 20:33:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents login data, used to authenticate users.
|
|
|
|
*/
|
|
|
|
export interface IUserLoginDTO {
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents data needed to update password for current user.
|
|
|
|
*/
|
|
|
|
export interface IChangePasswordDTO {
|
|
|
|
old_password: string;
|
|
|
|
new_password: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents password reset request data.
|
|
|
|
*/
|
2025-01-23 19:41:31 +03:00
|
|
|
export interface IRequestPasswordDTO {
|
|
|
|
email: string;
|
|
|
|
}
|
2025-01-21 20:33:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents password reset data.
|
|
|
|
*/
|
|
|
|
export interface IResetPasswordDTO {
|
|
|
|
password: string;
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents password token data.
|
|
|
|
*/
|
2025-01-23 19:41:31 +03:00
|
|
|
export interface IPasswordTokenDTO {
|
|
|
|
token: string;
|
|
|
|
}
|
2025-01-21 20:33:05 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Authentication API.
|
|
|
|
*/
|
|
|
|
export const authApi = {
|
|
|
|
baseKey: 'auth',
|
|
|
|
|
|
|
|
getAuthQueryOptions: () => {
|
|
|
|
return queryOptions({
|
|
|
|
queryKey: [authApi.baseKey, 'user'],
|
2025-01-23 19:41:31 +03:00
|
|
|
staleTime: DELAYS.staleLong,
|
2025-01-21 20:33:05 +03:00
|
|
|
queryFn: meta =>
|
|
|
|
axiosInstance
|
|
|
|
.get<ICurrentUser>('/users/api/auth', {
|
|
|
|
signal: meta.signal
|
|
|
|
})
|
|
|
|
.then(response => (response.data.id === null ? null : response.data)),
|
2025-01-23 19:41:31 +03:00
|
|
|
placeholderData: null
|
2025-01-21 20:33:05 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
logout: () => axiosInstance.post('/users/api/logout'),
|
|
|
|
login: (data: IUserLoginDTO) => axiosInstance.post('/users/api/login', data),
|
|
|
|
changePassword: (data: IChangePasswordDTO) => axiosInstance.post('/users/api/change-password', data),
|
|
|
|
requestPasswordReset: (data: IRequestPasswordDTO) => axiosInstance.post('/users/api/password-reset', data),
|
|
|
|
validatePasswordToken: (data: IPasswordTokenDTO) => axiosInstance.post('/users/api/password-reset/validate', data),
|
|
|
|
resetPassword: (data: IResetPasswordDTO) => axiosInstance.post('/users/api/password-reset/confirm', data)
|
|
|
|
};
|