Snai3i-LandingPage-FormBuilder / frontend / src / app / backend / endpoints / auth.ts
auth.ts
Raw
import api from '..';

const USERS_API = 'api/admin';

export const usersApi = api.injectEndpoints({
  endpoints: (build) => ({
    login: build.mutation<ResponseI<UserI>, UserAuthI>({
      query: (userAuth) => ({
        url: `${USERS_API}/login`,
        method: 'POST',
        body: userAuth,
      }),
    }),
    register: build.mutation<ResponseI<UserI>, UserRegisterI>({
      query: (userRegister) => ({
        url: `${USERS_API}/register`,
        method: 'POST',
        body: userRegister,
      }),
      invalidatesTags: ['users'],
    }),
    logout: build.mutation<ResponseI<null>, void>({
      query: () => ({
        url: `${USERS_API}/logout`,
        method: 'POST',
      }),
    }),
    getUser: build.mutation<ResponseI<UserI>, void>({
      query: () => ({
        url: `${USERS_API}/profile`,
        method: 'GET',
      }),
    }),
    getAllUsers: build.query<ResponseI<UserI[]>, void>({
      query: () => ({
        url: `${USERS_API}/users`,
        method: 'GET',
      }),
      providesTags: ['users'],
    }),
    resetPassword: build.mutation<
      ResponseI<UserI>,
      { userId: string; password: string }
    >({
      query: (body) => ({
        url: `${USERS_API}/password`,
        method: 'PUT',
        body,
      }),
    }),
    updateProfile: build.mutation<
      ResponseI<UserI>,
      UserUpdateI
    >({
      query: (body) => ({
        url: `${USERS_API}/profile`,
        method: 'PUT',
        body,
      }),
      invalidatesTags: ['users'],
    }),
    delete: build.mutation<ResponseI<null>, string>({
      query: (userId) => ({
        url: `${USERS_API}/delete`,
        method: 'DELETE',
        body: { userId },
      }),
      invalidatesTags: ['users'],
    }),
  }),
});

export const {
  useLoginMutation,
  useRegisterMutation,
  useLogoutMutation,
  useGetUserMutation,
  useGetAllUsersQuery,
  useResetPasswordMutation,
  useDeleteMutation,
  useUpdateProfileMutation,
} = usersApi;