mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-01 21:28:23 +00:00
refactor: streamline authentication flow by removing unused code, replacing custom auth utilities with NextAuth, and updating session handling in components
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 5m56s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 5m56s
This commit is contained in:
@ -10,6 +10,11 @@ const formSchema = z.object({
|
||||
.regex(/^[7|9][0-9]{2}-[0-9]{4}$/, "Please enter a valid phone number"),
|
||||
});
|
||||
|
||||
type FilterUserResponse = {
|
||||
ok: boolean;
|
||||
verified: boolean;
|
||||
};
|
||||
|
||||
export async function signin(previousState: ActionState, formData: FormData) {
|
||||
const phoneNumber = formData.get("phoneNumber") as string;
|
||||
const result = formSchema.safeParse({ phoneNumber });
|
||||
@ -31,7 +36,28 @@ export async function signin(previousState: ActionState, formData: FormData) {
|
||||
const FORMATTED_MOBILE_NUMBER: string = `${phoneNumber.split("-").join("")}`;
|
||||
console.log({ FORMATTED_MOBILE_NUMBER });
|
||||
|
||||
const userExistsResponse = await fetch(
|
||||
const user = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/api/auth/users/filter/?mobile=${FORMATTED_MOBILE_NUMBER}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
},
|
||||
);
|
||||
const userData = (await user.json()) as FilterUserResponse;
|
||||
if (!userData?.ok) {
|
||||
return redirect(`/auth/signup?phone_number=${phoneNumber}`);
|
||||
}
|
||||
if (!userData.verified) {
|
||||
return {
|
||||
message:
|
||||
"Your account is on pending verification. Please wait for a response from admin or contact shihaam.",
|
||||
status: "error",
|
||||
};
|
||||
}
|
||||
|
||||
const sendOTPResponse = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/auth/mobile/`,
|
||||
{
|
||||
method: "POST",
|
||||
@ -43,23 +69,10 @@ export async function signin(previousState: ActionState, formData: FormData) {
|
||||
}),
|
||||
},
|
||||
);
|
||||
const userExists = await userExistsResponse.json();
|
||||
console.log("user exists", userExists);
|
||||
if (userExists?.non_field_errors) {
|
||||
return redirect(`/signup?phone_number=${phoneNumber}`);
|
||||
}
|
||||
const otpResponse = await sendOTPResponse.json();
|
||||
console.log("otpResponse", otpResponse);
|
||||
|
||||
if (!userExists?.verified)
|
||||
return {
|
||||
message:
|
||||
"Your account is on pending verification. Please wait for a response from admin or contact shihaam.",
|
||||
status: "error",
|
||||
};
|
||||
|
||||
// await authClient.phoneNumber.sendOtp({
|
||||
// phoneNumber: NUMBER_WITH_COUNTRY_CODE,
|
||||
// });
|
||||
redirect(`/verify-otp?phone_number=${FORMATTED_MOBILE_NUMBER}`);
|
||||
redirect(`/auth/verify-otp?phone_number=${FORMATTED_MOBILE_NUMBER}`);
|
||||
}
|
||||
|
||||
type ActionState = {
|
||||
|
Reference in New Issue
Block a user