// import { authMiddleware } from "@clerk/nextjs"; // import { NextResponse } from "next/server"; // // This example protects all routes including api/trpc routes // // Please edit this to allow other routes to be public as needed. // // See https://clerk.com/docs/references/nextjs/auth-middleware for more information about configuring your middleware // export default authMiddleware({ // publicRoutes: ["/api/:path*"], // ignoredRoutes: ["/api/validate/:path*"], // beforeAuth: async (req) => { // // if (req.url.startsWith("https://localhost:3000/api") /*&& !req.url.startsWith("https://localhost:3000/api/validate/")*/) { // // const apiKey = req.headers.get("x-api-key") as string // // let api = null // // if (apiKey != null) { // // console.log("API KEY:", apiKey) // // api = await fetch("http://localhost:3000/api/validate") // // } // // if (api == null) { // // console.log("Invalid API key attempted") // // return NextResponse.rewrite( // // `${req.nextUrl.protocol}//${req.nextUrl.host}`, // // { // // status: 401, // // headers: { // // "WWW-Authenticate": 'Basic realm="Secure Area"', // // }, // // } // // ); // // } // // } // return NextResponse.next(); // } // }); // export const config = { // matcher: ["/((?!.*\\..*|_next).*)", "/", "/(trpc)(.*)"], // }; import { NextResponse } from "next/server"; import { redirect } from 'next/navigation'; import { withAuth } from 'next-auth/middleware'; import { getServerSession } from "next-auth"; export default withAuth( async function middleware(req) { // if (!req.url.startsWith("https://hermes.goblincaves.com")) { // return redirect("https://hermes.goblincaves.com") // } //console.log(req.nextauth) //console.log(req.nextauth.token) // if (req.nextUrl.pathname.startsWith("/api/auth")) { // //console.log("Auth API reached") // } else if (req.nextUrl.pathname.startsWith("/api")) { // //console.log("API reached") // const apiKey = req.headers.get("x-api-key") as string // let api = null // if (apiKey != null) { // //console.log("API KEY:", apiKey) // api = await fetch("http://localhost:3000/api/validate") // } // if (api == null || (await api.text()) == "null") { // //console.log("Invalid API key attempted") // return NextResponse.rewrite( // `${req.nextUrl.protocol}//${req.nextUrl.host}`, // { // status: 401, // headers: { // "WWW-Authenticate": 'Basic realm="Secure Area"', // }, // } // ); // } // } }, { callbacks: { authorized: async ({ req, token }) => req.nextUrl.pathname?.slice(0, 4) === '/api' || !!token } });