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

125 lines
3.0 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-02-03 18:17:07 +03:00
import { axiosGet, axiosPatch, axiosPost } from '@/backend/apiTransport';
2025-01-23 19:41:31 +03:00
import { DELAYS } from '@/backend/configuration';
import { errorMsg, infoMsg } from '@/utils/labels';
/**
* Represents CurrentUser information.
*/
export interface ICurrentUser {
2025-02-12 13:07:26 +03:00
id: number | null;
username: string;
is_staff: boolean;
2025-02-12 13:07:26 +03:00
editor: number[];
}
2025-01-21 20:33:05 +03:00
/**
* Represents login data, used to authenticate users.
*/
export const schemaUserLogin = z.object({
username: z.string().nonempty(errorMsg.requiredField),
password: z.string().nonempty(errorMsg.requiredField)
2025-01-30 21:00:59 +03:00
});
/**
* Represents login data, used to authenticate users.
*/
export type IUserLoginDTO = z.infer<typeof schemaUserLogin>;
2025-01-21 20:33:05 +03:00
/**
* Represents data needed to update password for current user.
*/
export const schemaChangePassword = z
2025-02-03 18:17:07 +03:00
.object({
old_password: z.string().nonempty(errorMsg.requiredField),
new_password: z.string().nonempty(errorMsg.requiredField),
new_password2: z.string().nonempty(errorMsg.requiredField)
2025-02-03 18:17:07 +03:00
})
.refine(schema => schema.new_password === schema.new_password2, {
path: ['new_password2'],
message: errorMsg.passwordsMismatch
2025-02-03 18:17:07 +03:00
})
.refine(schema => schema.old_password !== schema.new_password, {
path: ['new_password'],
message: errorMsg.passwordsSame
2025-02-03 18:17:07 +03:00
});
/**
* Represents data needed to update password for current user.
*/
export type IChangePasswordDTO = z.infer<typeof schemaChangePassword>;
2025-01-21 20:33:05 +03:00
/**
* 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) =>
2025-02-03 18:17:07 +03:00
axiosPatch({
endpoint: '/users/api/change-password',
request: {
data: data,
successMessage: infoMsg.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
};