Portal/rsconcept/frontend/src/backend/auth/api.ts

101 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-01-21 20:33:05 +03:00
import { queryOptions } from '@tanstack/react-query';
2025-01-30 21:00:59 +03:00
import { z } from 'zod';
2025-01-21 20:33:05 +03:00
2025-01-28 23:23:03 +03:00
import { axiosGet, axiosPost } from '@/backend/apiTransport';
2025-01-23 19:41:31 +03:00
import { DELAYS } from '@/backend/configuration';
import { ICurrentUser } from '@/models/user';
import { errors, information } from '@/utils/labels';
2025-01-21 20:33:05 +03:00
/**
* Represents login data, used to authenticate users.
*/
2025-01-30 21:00:59 +03:00
export const UserLoginSchema = z.object({
username: z.string().nonempty(errors.requiredField),
password: z.string().nonempty(errors.requiredField)
2025-01-30 21:00:59 +03:00
});
/**
* Represents login data, used to authenticate users.
*/
export type IUserLoginDTO = z.infer<typeof UserLoginSchema>;
2025-01-21 20:33:05 +03:00
/**
* 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 =>
axiosGet<ICurrentUser>({
endpoint: '/users/api/auth',
options: { signal: meta.signal }
})
2025-01-21 20:33:05 +03:00
});
},
logout: () => axiosPost({ endpoint: '/users/api/logout' }),
login: (data: IUserLoginDTO) =>
axiosPost({
endpoint: '/users/api/login',
request: { data: data }
}),
changePassword: (data: IChangePasswordDTO) =>
axiosPost({
endpoint: '/users/api/change-password',
request: {
data: data,
successMessage: information.changesSaved
}
}),
requestPasswordReset: (data: IRequestPasswordDTO) =>
axiosPost({
endpoint: '/users/api/password-reset',
request: { data: data }
}),
validatePasswordToken: (data: IPasswordTokenDTO) =>
axiosPost({
endpoint: '/users/api/password-reset/validate',
request: { data: data }
}),
resetPassword: (data: IResetPasswordDTO) =>
axiosPost({
endpoint: '/users/api/password-reset/confirm',
request: { data: data }
})
2025-01-21 20:33:05 +03:00
};