mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-01 21:28:23 +00:00
refactor: update authentication flow to use NextAuth, replace better-auth with axios for API calls, and clean up unused code
This commit is contained in:
@ -1,10 +1,7 @@
|
||||
"use server";
|
||||
|
||||
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 { redirect } from "next/navigation";
|
||||
import { z } from "zod";
|
||||
@ -34,7 +31,7 @@ export async function signin(previousState: ActionState, formData: FormData) {
|
||||
};
|
||||
}
|
||||
const FORMATTED_MOBILE_NUMBER: string = `${phoneNumber.split("-").join("")}`;
|
||||
console.log(FORMATTED_MOBILE_NUMBER);
|
||||
console.log({ FORMATTED_MOBILE_NUMBER });
|
||||
const userExistsResponse = await fetch(
|
||||
`${process.env.SARLINK_API_BASE_URL}/auth/mobile/`,
|
||||
{
|
||||
@ -48,7 +45,7 @@ export async function signin(previousState: ActionState, formData: FormData) {
|
||||
},
|
||||
);
|
||||
const userExists = await userExistsResponse.json();
|
||||
console.log(userExists.non_field_errors);
|
||||
console.log("user exists", userExists);
|
||||
if (userExists?.non_field_errors) {
|
||||
return redirect(`/signup?phone_number=${phoneNumber}`);
|
||||
}
|
||||
@ -75,7 +72,6 @@ export async function signup(_actionState: ActionState, formData: FormData) {
|
||||
const data = Object.fromEntries(formData.entries());
|
||||
const parsedData = signUpFormSchema.safeParse(data);
|
||||
// get phone number from /signup?phone_number=999-1231
|
||||
const headersList = await headers();
|
||||
|
||||
console.log("DATA ON SERVER SIDE", data);
|
||||
|
||||
@ -87,83 +83,82 @@ export async function signup(_actionState: ActionState, formData: FormData) {
|
||||
};
|
||||
}
|
||||
|
||||
const idCardExists = await prisma.user.findFirst({
|
||||
where: {
|
||||
id_card: parsedData.data.id_card,
|
||||
},
|
||||
});
|
||||
// const idCardExists = await prisma.user.findFirst({
|
||||
// where: {
|
||||
// id_card: parsedData.data.id_card,
|
||||
// },
|
||||
// });
|
||||
|
||||
if (idCardExists) {
|
||||
return {
|
||||
message: "ID card already exists.",
|
||||
payload: formData,
|
||||
db_error: "id_card",
|
||||
};
|
||||
}
|
||||
// if (idCardExists) {
|
||||
// return {
|
||||
// message: "ID card already exists.",
|
||||
// payload: formData,
|
||||
// db_error: "id_card",
|
||||
// };
|
||||
// }
|
||||
|
||||
const phoneNumberExists = await prisma.user.findFirst({
|
||||
where: {
|
||||
phoneNumber: parsedData.data.phone_number,
|
||||
},
|
||||
});
|
||||
// const phoneNumberExists = await prisma.user.findFirst({
|
||||
// where: {
|
||||
// phoneNumber: parsedData.data.phone_number,
|
||||
// },
|
||||
// });
|
||||
|
||||
if (phoneNumberExists) {
|
||||
return {
|
||||
message: "Phone number already exists.",
|
||||
payload: formData,
|
||||
db_error: "phone_number",
|
||||
};
|
||||
}
|
||||
// if (phoneNumberExists) {
|
||||
// return {
|
||||
// message: "Phone number already exists.",
|
||||
// payload: formData,
|
||||
// db_error: "phone_number",
|
||||
// };
|
||||
// }
|
||||
|
||||
const newUser = await prisma.user.create({
|
||||
data: {
|
||||
name: parsedData.data.name,
|
||||
islandId: parsedData.data.island_id,
|
||||
atollId: parsedData.data.atoll_id,
|
||||
address: parsedData.data.address,
|
||||
id_card: parsedData.data.id_card,
|
||||
dob: new Date(parsedData.data.dob),
|
||||
role: "USER",
|
||||
accNo: parsedData.data.accNo,
|
||||
phoneNumber: parsedData.data.phone_number,
|
||||
},
|
||||
});
|
||||
const isValidPerson = await VerifyUserDetails({ user: newUser });
|
||||
// const newUser = await prisma.user.create({
|
||||
// data: {
|
||||
// name: parsedData.data.name,
|
||||
// islandId: parsedData.data.island_id,
|
||||
// atollId: parsedData.data.atoll_id,
|
||||
// address: parsedData.data.address,
|
||||
// id_card: parsedData.data.id_card,
|
||||
// dob: new Date(parsedData.data.dob),
|
||||
// role: "USER",
|
||||
// accNo: parsedData.data.accNo,
|
||||
// phoneNumber: parsedData.data.phone_number,
|
||||
// },
|
||||
// });
|
||||
// const isValidPerson = await VerifyUserDetails({ user: newUser });
|
||||
|
||||
if (!isValidPerson) {
|
||||
await SendUserRejectionDetailSMS({
|
||||
details: `
|
||||
A new user has requested for verification. \n
|
||||
USER DETAILS:
|
||||
Name: ${parsedData.data.name}
|
||||
Address: ${parsedData.data.address}
|
||||
ID Card: ${parsedData.data.id_card}
|
||||
DOB: ${parsedData.data.dob.toLocaleDateString("en-US", {
|
||||
month: "short",
|
||||
day: "2-digit",
|
||||
year: "numeric",
|
||||
})}
|
||||
ACC No: ${parsedData.data.accNo}\n\nVerify the user with the following link: ${process.env.BETTER_AUTH_URL}/users/${newUser.id}/verify
|
||||
`,
|
||||
phoneNumber: process.env.ADMIN_PHONENUMBER ?? "",
|
||||
});
|
||||
return {
|
||||
message:
|
||||
"Your account has been requested for verification. Please wait for a response from admin.",
|
||||
payload: formData,
|
||||
db_error: "invalidPersonValidation",
|
||||
};
|
||||
}
|
||||
// if (!isValidPerson) {
|
||||
// await SendUserRejectionDetailSMS({
|
||||
// details: `
|
||||
// A new user has requested for verification. \n
|
||||
// USER DETAILS:
|
||||
// Name: ${parsedData.data.name}
|
||||
// Address: ${parsedData.data.address}
|
||||
// ID Card: ${parsedData.data.id_card}
|
||||
// DOB: ${parsedData.data.dob.toLocaleDateString("en-US", {
|
||||
// month: "short",
|
||||
// day: "2-digit",
|
||||
// year: "numeric",
|
||||
// })}
|
||||
// ACC No: ${parsedData.data.accNo}\n\nVerify the user with the following link: ${process.env.BETTER_AUTH_URL}/users/${newUser.id}/verify
|
||||
// `,
|
||||
// phoneNumber: process.env.ADMIN_PHONENUMBER ?? "",
|
||||
// });
|
||||
// return {
|
||||
// message:
|
||||
// "Your account has been requested for verification. Please wait for a response from admin.",
|
||||
// payload: formData,
|
||||
// db_error: "invalidPersonValidation",
|
||||
// };
|
||||
|
||||
if (isValidPerson) {
|
||||
await authClient.phoneNumber.sendOtp({
|
||||
phoneNumber: newUser.phoneNumber,
|
||||
});
|
||||
}
|
||||
redirect(
|
||||
`/verify-otp?phone_number=${encodeURIComponent(newUser.phoneNumber)}`,
|
||||
);
|
||||
return { message: "User created successfully" };
|
||||
// if (isValidPerson) {
|
||||
// await authClient.phoneNumber.sendOtp({
|
||||
// phoneNumber: newUser.phoneNumber,
|
||||
// });
|
||||
// }
|
||||
// redirect(
|
||||
// `/verify-otp?phone_number=${encodeURIComponent(newUser.phoneNumber)}`,
|
||||
// );
|
||||
// return { message: "User created successfully" };
|
||||
}
|
||||
|
||||
export const sendOtp = async (phoneNumber: string, code: string) => {
|
||||
|
Reference in New Issue
Block a user