mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-01 21:28:23 +00:00
refactor: migrate authentication and signup flow to use external API and improve type safety
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m58s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 1m58s
This commit is contained in:
@ -4,8 +4,8 @@ import { authClient } from "@/lib/auth-client";
|
||||
import prisma from "@/lib/db";
|
||||
import { VerifyUserDetails } from "@/lib/person";
|
||||
import { signUpFormSchema } from "@/lib/schemas";
|
||||
import { phoneNumber } from "better-auth/plugins";
|
||||
import { headers } from "next/headers";
|
||||
// import type { User } from "@prisma/client";
|
||||
import { redirect } from "next/navigation";
|
||||
import { z } from "zod";
|
||||
import { SendUserRejectionDetailSMS } from "./user-actions";
|
||||
@ -33,14 +33,23 @@ export async function signin(previousState: ActionState, formData: FormData) {
|
||||
status: "error",
|
||||
};
|
||||
}
|
||||
const NUMBER_WITH_COUNTRY_CODE: string = `+960${phoneNumber.split("-").join("")}`;
|
||||
|
||||
const userExists = await prisma.user.findUnique({
|
||||
where: {
|
||||
phoneNumber: NUMBER_WITH_COUNTRY_CODE,
|
||||
const FORMATTED_MOBILE_NUMBER: string = `${phoneNumber.split("-").join("")}`;
|
||||
console.log(FORMATTED_MOBILE_NUMBER);
|
||||
const userExistsResponse = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/auth/mobile/`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
mobile: FORMATTED_MOBILE_NUMBER,
|
||||
}),
|
||||
},
|
||||
});
|
||||
if (!userExists) {
|
||||
);
|
||||
const userExists = await userExistsResponse.json();
|
||||
console.log(userExists.non_field_errors);
|
||||
if (userExists?.non_field_errors) {
|
||||
return redirect(`/signup?phone_number=${phoneNumber}`);
|
||||
}
|
||||
|
||||
@ -51,12 +60,10 @@ export async function signin(previousState: ActionState, formData: FormData) {
|
||||
status: "error",
|
||||
};
|
||||
|
||||
await authClient.phoneNumber.sendOtp({
|
||||
phoneNumber: NUMBER_WITH_COUNTRY_CODE,
|
||||
});
|
||||
redirect(
|
||||
`/verify-otp?phone_number=${encodeURIComponent(NUMBER_WITH_COUNTRY_CODE)}`,
|
||||
);
|
||||
// await authClient.phoneNumber.sendOtp({
|
||||
// phoneNumber: NUMBER_WITH_COUNTRY_CODE,
|
||||
// });
|
||||
redirect(`/verify-otp?phone_number=${FORMATTED_MOBILE_NUMBER}`);
|
||||
}
|
||||
|
||||
type ActionState = {
|
||||
@ -69,11 +76,8 @@ export async function signup(_actionState: ActionState, formData: FormData) {
|
||||
const parsedData = signUpFormSchema.safeParse(data);
|
||||
// get phone number from /signup?phone_number=999-1231
|
||||
const headersList = await headers();
|
||||
const referer = headersList.get("referer");
|
||||
const number = referer?.split("?")[1]?.split("=")[1];
|
||||
let NUMBER_WITH_COUNTRY_CODE: string;
|
||||
|
||||
console.log(data);
|
||||
console.log("DATA ON SERVER SIDE", data);
|
||||
|
||||
if (!parsedData.success) {
|
||||
return {
|
||||
@ -83,13 +87,6 @@ export async function signup(_actionState: ActionState, formData: FormData) {
|
||||
};
|
||||
}
|
||||
|
||||
if (number) {
|
||||
NUMBER_WITH_COUNTRY_CODE = `+960${number.split("-").join("")}`;
|
||||
} else {
|
||||
NUMBER_WITH_COUNTRY_CODE = `+960${parsedData.data.phone_number.split("-").join("")}`;
|
||||
}
|
||||
console.log({ NUMBER_WITH_COUNTRY_CODE });
|
||||
|
||||
const idCardExists = await prisma.user.findFirst({
|
||||
where: {
|
||||
id_card: parsedData.data.id_card,
|
||||
@ -106,7 +103,7 @@ export async function signup(_actionState: ActionState, formData: FormData) {
|
||||
|
||||
const phoneNumberExists = await prisma.user.findFirst({
|
||||
where: {
|
||||
phoneNumber: NUMBER_WITH_COUNTRY_CODE,
|
||||
phoneNumber: parsedData.data.phone_number,
|
||||
},
|
||||
});
|
||||
|
||||
@ -128,7 +125,7 @@ export async function signup(_actionState: ActionState, formData: FormData) {
|
||||
dob: new Date(parsedData.data.dob),
|
||||
role: "USER",
|
||||
accNo: parsedData.data.accNo,
|
||||
phoneNumber: NUMBER_WITH_COUNTRY_CODE,
|
||||
phoneNumber: parsedData.data.phone_number,
|
||||
},
|
||||
});
|
||||
const isValidPerson = await VerifyUserDetails({ user: newUser });
|
||||
|
Reference in New Issue
Block a user