mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-23 13:11:27 +10:00
refactor(v4.0.0-alpha): beginning of a new era
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
import { MessageDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const resendVerificationEmail = async () => {
|
||||
const response = await axios.post<MessageDto, AxiosResponse<MessageDto>>(
|
||||
"/auth/verify-email/resend",
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useResendVerificationEmail = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: resendVerificationEmailFn,
|
||||
} = useMutation({
|
||||
mutationFn: resendVerificationEmail,
|
||||
});
|
||||
|
||||
return { resendVerificationEmail: resendVerificationEmailFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,25 @@
|
||||
import { MessageDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const verifyEmail = async (data: { token: string }) => {
|
||||
const response = await axios.post<MessageDto, AxiosResponse<MessageDto>>(
|
||||
`/auth/verify-email?token=${data.token}`,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useVerifyEmail = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: verifyEmailFn,
|
||||
} = useMutation({
|
||||
mutationFn: verifyEmail,
|
||||
});
|
||||
|
||||
return { verifyEmail: verifyEmailFn, loading, error };
|
||||
};
|
||||
20
apps/client/src/services/auth/index.ts
Normal file
20
apps/client/src/services/auth/index.ts
Normal file
@ -0,0 +1,20 @@
|
||||
export * from "./login";
|
||||
export * from "./logout";
|
||||
export * from "./refresh";
|
||||
export * from "./register";
|
||||
export * from "./update-password";
|
||||
|
||||
// Email Verification
|
||||
export * from "./email-verification/resend-verify-email";
|
||||
export * from "./email-verification/verify-email";
|
||||
|
||||
// Password Recovery
|
||||
export * from "./password-recovery/forgot-password";
|
||||
export * from "./password-recovery/reset-password";
|
||||
|
||||
// Two Factor Authentication
|
||||
export * from "./two-factor-authentication/backup-otp";
|
||||
export * from "./two-factor-authentication/disable";
|
||||
export * from "./two-factor-authentication/enable";
|
||||
export * from "./two-factor-authentication/setup";
|
||||
export * from "./two-factor-authentication/verify-otp";
|
||||
40
apps/client/src/services/auth/login.ts
Normal file
40
apps/client/src/services/auth/login.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { AuthResponseDto, LoginDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
import { queryClient } from "@/client/libs/query-client";
|
||||
import { useAuthStore } from "@/client/stores/auth";
|
||||
|
||||
export const login = async (data: LoginDto) => {
|
||||
const response = await axios.post<AuthResponseDto, AxiosResponse<AuthResponseDto>, LoginDto>(
|
||||
"/auth/login",
|
||||
data,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useLogin = () => {
|
||||
const navigate = useNavigate();
|
||||
const setUser = useAuthStore((state) => state.setUser);
|
||||
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: loginFn,
|
||||
} = useMutation({
|
||||
mutationFn: login,
|
||||
onSuccess: (data) => {
|
||||
if (data.status === "2fa_required") {
|
||||
return navigate("/auth/verify-otp");
|
||||
}
|
||||
|
||||
setUser(data.user);
|
||||
queryClient.setQueryData(["user"], data.user);
|
||||
},
|
||||
});
|
||||
|
||||
return { login: loginFn, loading, error };
|
||||
};
|
||||
31
apps/client/src/services/auth/logout.ts
Normal file
31
apps/client/src/services/auth/logout.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
import { queryClient } from "@/client/libs/query-client";
|
||||
import { useAuthStore } from "@/client/stores/auth";
|
||||
|
||||
export const logout = async () => {
|
||||
await axios.post("/auth/logout");
|
||||
};
|
||||
|
||||
export const useLogout = () => {
|
||||
const setUser = useAuthStore((state) => state.setUser);
|
||||
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: logoutFn,
|
||||
} = useMutation({
|
||||
mutationFn: logout,
|
||||
onSuccess: () => {
|
||||
setUser(null);
|
||||
queryClient.setQueryData(["user"], null);
|
||||
},
|
||||
onError: () => {
|
||||
setUser(null);
|
||||
queryClient.setQueryData(["user"], null);
|
||||
},
|
||||
});
|
||||
|
||||
return { logout: logoutFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,26 @@
|
||||
import { ForgotPasswordDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const forgotPassword = async (data: ForgotPasswordDto) => {
|
||||
const response = await axios.post<void, AxiosResponse<void>, ForgotPasswordDto>(
|
||||
"/auth/forgot-password",
|
||||
data,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useForgotPassword = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: forgotPasswordFn,
|
||||
} = useMutation({
|
||||
mutationFn: forgotPassword,
|
||||
});
|
||||
|
||||
return { forgotPassword: forgotPasswordFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,26 @@
|
||||
import { ResetPasswordDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const resetPassword = async (data: ResetPasswordDto) => {
|
||||
const response = await axios.post<void, AxiosResponse<void>, ResetPasswordDto>(
|
||||
"/auth/reset-password",
|
||||
data,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useResetPassword = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: resetPasswordFn,
|
||||
} = useMutation({
|
||||
mutationFn: resetPassword,
|
||||
});
|
||||
|
||||
return { resetPassword: resetPasswordFn, loading, error };
|
||||
};
|
||||
8
apps/client/src/services/auth/refresh.ts
Normal file
8
apps/client/src/services/auth/refresh.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { MessageDto } from "@reactive-resume/dto";
|
||||
import { AxiosInstance, AxiosResponse } from "axios";
|
||||
|
||||
export const refresh = async (axios: AxiosInstance) => {
|
||||
const response = await axios.post<MessageDto, AxiosResponse<MessageDto>>("/auth/refresh");
|
||||
|
||||
return response.data;
|
||||
};
|
||||
34
apps/client/src/services/auth/register.ts
Normal file
34
apps/client/src/services/auth/register.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import { AuthResponseDto, RegisterDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
import { queryClient } from "@/client/libs/query-client";
|
||||
import { useAuthStore } from "@/client/stores/auth";
|
||||
|
||||
export const register = async (data: RegisterDto) => {
|
||||
const response = await axios.post<AuthResponseDto, AxiosResponse<AuthResponseDto>, RegisterDto>(
|
||||
"/auth/register",
|
||||
data,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useRegister = () => {
|
||||
const setUser = useAuthStore((state) => state.setUser);
|
||||
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: registerFn,
|
||||
} = useMutation({
|
||||
mutationFn: register,
|
||||
onSuccess: (data) => {
|
||||
setUser(data.user);
|
||||
queryClient.setQueryData(["user"], data.user);
|
||||
},
|
||||
});
|
||||
|
||||
return { register: registerFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,43 @@
|
||||
import { AuthResponseDto, TwoFactorBackupDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosError, AxiosResponse } from "axios";
|
||||
|
||||
import { toast } from "@/client/hooks/use-toast";
|
||||
import { axios } from "@/client/libs/axios";
|
||||
import { queryClient } from "@/client/libs/query-client";
|
||||
import { useAuthStore } from "@/client/stores/auth";
|
||||
|
||||
export const backupOtp = async (data: TwoFactorBackupDto) => {
|
||||
const response = await axios.post<
|
||||
AuthResponseDto,
|
||||
AxiosResponse<AuthResponseDto>,
|
||||
TwoFactorBackupDto
|
||||
>("/auth/2fa/backup", data);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useBackupOtp = () => {
|
||||
const setUser = useAuthStore((state) => state.setUser);
|
||||
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: backupOtpFn,
|
||||
} = useMutation({
|
||||
mutationFn: backupOtp,
|
||||
onSuccess: (data) => {
|
||||
setUser(data.user);
|
||||
queryClient.setQueryData(["user"], data.user);
|
||||
},
|
||||
onError: (error) => {
|
||||
if (error instanceof AxiosError) {
|
||||
const message = error.response?.data?.message || error.message;
|
||||
|
||||
toast({ variant: "error", title: message });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return { backupOtp: backupOtpFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,23 @@
|
||||
import { MessageDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const disable2FA = async () => {
|
||||
const response = await axios.post<MessageDto, AxiosResponse<MessageDto>>("/auth/2fa/disable");
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useDisable2FA = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: disable2FAFn,
|
||||
} = useMutation({
|
||||
mutationFn: disable2FA,
|
||||
});
|
||||
|
||||
return { disable2FA: disable2FAFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,26 @@
|
||||
import { BackupCodesDto, TwoFactorDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const enable2FA = async (data: TwoFactorDto) => {
|
||||
const response = await axios.post<BackupCodesDto, AxiosResponse<BackupCodesDto>, TwoFactorDto>(
|
||||
"/auth/2fa/enable",
|
||||
data,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useEnable2FA = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: enable2FAFn,
|
||||
} = useMutation({
|
||||
mutationFn: enable2FA,
|
||||
});
|
||||
|
||||
return { enable2FA: enable2FAFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,23 @@
|
||||
import { MessageDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const setup2FA = async () => {
|
||||
const response = await axios.post<MessageDto, AxiosResponse<MessageDto>>("/auth/2fa/setup");
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useSetup2FA = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: setup2FAFn,
|
||||
} = useMutation({
|
||||
mutationFn: setup2FA,
|
||||
});
|
||||
|
||||
return { setup2FA: setup2FAFn, loading, error };
|
||||
};
|
||||
@ -0,0 +1,42 @@
|
||||
import { AuthResponseDto, TwoFactorDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosError, AxiosResponse } from "axios";
|
||||
|
||||
import { toast } from "@/client/hooks/use-toast";
|
||||
import { axios } from "@/client/libs/axios";
|
||||
import { queryClient } from "@/client/libs/query-client";
|
||||
import { useAuthStore } from "@/client/stores/auth";
|
||||
|
||||
export const verifyOtp = async (data: TwoFactorDto) => {
|
||||
const response = await axios.post<AuthResponseDto, AxiosResponse<AuthResponseDto>, TwoFactorDto>(
|
||||
"/auth/2fa/verify",
|
||||
data,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useVerifyOtp = () => {
|
||||
const setUser = useAuthStore((state) => state.setUser);
|
||||
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: verifyOtpFn,
|
||||
} = useMutation({
|
||||
mutationFn: verifyOtp,
|
||||
onSuccess: (data) => {
|
||||
setUser(data.user);
|
||||
queryClient.setQueryData(["user"], data.user);
|
||||
},
|
||||
onError: (error) => {
|
||||
if (error instanceof AxiosError) {
|
||||
const message = error.response?.data?.message || error.message;
|
||||
|
||||
toast({ variant: "error", title: message });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return { verifyOtp: verifyOtpFn, loading, error };
|
||||
};
|
||||
26
apps/client/src/services/auth/update-password.ts
Normal file
26
apps/client/src/services/auth/update-password.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { MessageDto, UpdatePasswordDto } from "@reactive-resume/dto";
|
||||
import { useMutation } from "@tanstack/react-query";
|
||||
import { AxiosResponse } from "axios";
|
||||
|
||||
import { axios } from "@/client/libs/axios";
|
||||
|
||||
export const updatePassword = async (data: UpdatePasswordDto) => {
|
||||
const response = await axios.patch<MessageDto, AxiosResponse<MessageDto>, UpdatePasswordDto>(
|
||||
"/auth/password",
|
||||
data,
|
||||
);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
export const useUpdatePassword = () => {
|
||||
const {
|
||||
error,
|
||||
isPending: loading,
|
||||
mutateAsync: updatePasswordFn,
|
||||
} = useMutation({
|
||||
mutationFn: updatePassword,
|
||||
});
|
||||
|
||||
return { updatePassword: updatePasswordFn, loading, error };
|
||||
};
|
||||
Reference in New Issue
Block a user