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

This commit is contained in:
2025-03-28 22:24:45 +05:00
parent 99c5fef748
commit ef9f032366
10 changed files with 291 additions and 323 deletions

View File

@ -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 = {

View File

@ -1,6 +1,5 @@
"use server";
import prisma from "@/lib/db";
import type { GroupProfile, MacAddress, OmadaResponse } from "@/lib/types";
import { formatMacAddress } from "@/lib/utils";
import { revalidatePath } from "next/cache";
@ -124,11 +123,11 @@ export async function blockDevice({
if (!macAddress) {
throw new Error("macAddress is a required parameter");
}
const device = await prisma.device.findFirst({
where: {
mac: macAddress,
},
});
// const device = await prisma.device.findFirst({
// where: {
// mac: macAddress,
// },
// });
try {
const baseUrl: string = process.env.OMADA_BASE_URL || "";
const url: string = `${baseUrl}/api/v2/sites/${process.env.OMADA_SITE_ID}/cmd/clients/${formatMacAddress(macAddress)}/${type}`;
@ -146,16 +145,16 @@ export async function blockDevice({
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
await prisma.device.update({
where: {
id: device?.id,
},
data: {
reasonForBlocking: type === "block" ? reason : "",
blocked: type === "block",
blockedBy: blockedBy,
},
});
// await prisma.device.update({
// where: {
// id: device?.id,
// },
// data: {
// reasonForBlocking: type === "block" ? reason : "",
// blocked: type === "block",
// blockedBy: blockedBy,
// },
// });
revalidatePath("/parental-control");
} catch (error) {
console.error("Error blocking device:", error);