refactor: replace custom authentication middleware with NextAuth, remove unused authentication pages, and update matcher configuration

This commit is contained in:
i701 2025-03-23 18:06:27 +05:00
parent 020d74c5e2
commit 32bb01b656
Signed by: i701
GPG Key ID: 54A0DA1E26D8E587
8 changed files with 34 additions and 37 deletions

View File

@ -32,6 +32,7 @@ export async function signin(previousState: ActionState, formData: FormData) {
} }
const FORMATTED_MOBILE_NUMBER: string = `${phoneNumber.split("-").join("")}`; const FORMATTED_MOBILE_NUMBER: string = `${phoneNumber.split("-").join("")}`;
console.log({ FORMATTED_MOBILE_NUMBER }); console.log({ FORMATTED_MOBILE_NUMBER });
const userExistsResponse = await fetch( const userExistsResponse = await fetch(
`${process.env.SARLINK_API_BASE_URL}/auth/mobile/`, `${process.env.SARLINK_API_BASE_URL}/auth/mobile/`,
{ {

View File

@ -1,5 +1,4 @@
import SignUpForm from "@/components/auth/signup-form"; import SignUpForm from "@/components/auth/signup-form";
import { getAtollsWithIslands } from "@/queries/atoll";
import Image from "next/image"; import Image from "next/image";
import { redirect } from "next/navigation"; import { redirect } from "next/navigation";

View File

@ -1,4 +0,0 @@
import { auth } from "@/app/auth";
import { toNextJsHandler } from "better-auth/next-js";
export const { GET, POST } = toNextJsHandler(auth.handler);

View File

@ -0,0 +1,5 @@
import { authOptions } from "@/app/auth";
import NextAuth from "next-auth";
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };

View File

@ -1,36 +1,20 @@
import type { Session } from "better-auth/types"; import { withAuth } from "next-auth/middleware";
import { type NextRequest, NextResponse } from "next/server";
export default async function authMiddleware(request: NextRequest) { export default withAuth(
const protocol = request.headers.get("x-forwarded-proto") || "http"; // `withAuth` augments your `Request` with the user's token.
const host = request.headers.get("host") || "localhost:3000"; function middleware(req) {},
);
try {
const response = await fetch(`${protocol}://${host}/api/auth/get-session`, {
method: "GET",
headers: {
cookie: request.headers.get("cookie") || "",
host: host,
},
next: { revalidate: 600 }, // Cache for 10 minutes (600 seconds)
});
if (!response.ok) {
throw new Error("Failed to fetch session");
}
const session: Session = await response.json();
if (!session) {
return NextResponse.redirect(new URL("/login", request.url));
}
return NextResponse.next();
} catch (error) {
console.log("Middleware error", error);
return NextResponse.redirect(new URL("/login", request.url));
}
}
export const config = { export const config = {
matcher: ["/devices", "/", "/payments", "/payments/:paymentId"], // https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api|_next/static|_next/image|favicon.ico|auth/|access-denied).*)",
],
}; };

12
queries/islands.ts Normal file
View File

@ -0,0 +1,12 @@
"use server";
export async function getIslands() {
const res = await fetch(`${process.env.SARLINK_API_BASE_URL}/islands/`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
const data = await res.json();
return data;
}