From 32bb01b656336f36fe2631b5095dc581695b3f0e Mon Sep 17 00:00:00 2001 From: i701 Date: Sun, 23 Mar 2025 18:06:27 +0500 Subject: [PATCH] refactor: replace custom authentication middleware with NextAuth, remove unused authentication pages, and update matcher configuration --- actions/auth-actions.ts | 1 + app/(auth)/{login => auth/signin}/page.tsx | 0 app/(auth)/{ => auth}/signup/page.tsx | 1 - app/(auth)/{ => auth}/verify-otp/page.tsx | 0 app/api/auth/[...all]/route.ts | 4 -- app/api/auth/[...nextauth]/route.ts | 5 +++ middleware.ts | 48 ++++++++-------------- queries/islands.ts | 12 ++++++ 8 files changed, 34 insertions(+), 37 deletions(-) rename app/(auth)/{login => auth/signin}/page.tsx (100%) rename app/(auth)/{ => auth}/signup/page.tsx (94%) rename app/(auth)/{ => auth}/verify-otp/page.tsx (100%) delete mode 100644 app/api/auth/[...all]/route.ts create mode 100644 app/api/auth/[...nextauth]/route.ts create mode 100644 queries/islands.ts diff --git a/actions/auth-actions.ts b/actions/auth-actions.ts index 6f8d155..ddccefc 100644 --- a/actions/auth-actions.ts +++ b/actions/auth-actions.ts @@ -32,6 +32,7 @@ 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( `${process.env.SARLINK_API_BASE_URL}/auth/mobile/`, { diff --git a/app/(auth)/login/page.tsx b/app/(auth)/auth/signin/page.tsx similarity index 100% rename from app/(auth)/login/page.tsx rename to app/(auth)/auth/signin/page.tsx diff --git a/app/(auth)/signup/page.tsx b/app/(auth)/auth/signup/page.tsx similarity index 94% rename from app/(auth)/signup/page.tsx rename to app/(auth)/auth/signup/page.tsx index faee80f..05d89b0 100644 --- a/app/(auth)/signup/page.tsx +++ b/app/(auth)/auth/signup/page.tsx @@ -1,5 +1,4 @@ import SignUpForm from "@/components/auth/signup-form"; -import { getAtollsWithIslands } from "@/queries/atoll"; import Image from "next/image"; import { redirect } from "next/navigation"; diff --git a/app/(auth)/verify-otp/page.tsx b/app/(auth)/auth/verify-otp/page.tsx similarity index 100% rename from app/(auth)/verify-otp/page.tsx rename to app/(auth)/auth/verify-otp/page.tsx diff --git a/app/api/auth/[...all]/route.ts b/app/api/auth/[...all]/route.ts deleted file mode 100644 index 24c5477..0000000 --- a/app/api/auth/[...all]/route.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { auth } from "@/app/auth"; -import { toNextJsHandler } from "better-auth/next-js"; - -export const { GET, POST } = toNextJsHandler(auth.handler); diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts new file mode 100644 index 0000000..29f8a06 --- /dev/null +++ b/app/api/auth/[...nextauth]/route.ts @@ -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 }; diff --git a/middleware.ts b/middleware.ts index 828efb8..f4d2e6d 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,36 +1,20 @@ -import type { Session } from "better-auth/types"; -import { type NextRequest, NextResponse } from "next/server"; +import { withAuth } from "next-auth/middleware"; -export default async function authMiddleware(request: NextRequest) { - const protocol = request.headers.get("x-forwarded-proto") || "http"; - const host = request.headers.get("host") || "localhost:3000"; - - 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 default withAuth( + // `withAuth` augments your `Request` with the user's token. + function middleware(req) {}, +); 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).*)", + ], }; diff --git a/queries/islands.ts b/queries/islands.ts new file mode 100644 index 0000000..04ca0b9 --- /dev/null +++ b/queries/islands.ts @@ -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; +}