mirror of
https://github.com/soconnor0919/hristudio.git
synced 2025-12-11 14:44:44 -05:00
Add authentication
This commit is contained in:
55
middleware.ts
Normal file
55
middleware.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { auth } from "./src/server/auth";
|
||||
import type { NextRequest } from "next/server";
|
||||
import type { Session } from "next-auth";
|
||||
|
||||
export default auth((req: NextRequest & { auth: Session | null }) => {
|
||||
const { nextUrl } = req;
|
||||
const isLoggedIn = !!req.auth;
|
||||
|
||||
// Define route patterns
|
||||
const isApiAuthRoute = nextUrl.pathname.startsWith("/api/auth");
|
||||
const isPublicRoute = ["/", "/auth/signin", "/auth/signup"].includes(
|
||||
nextUrl.pathname,
|
||||
);
|
||||
const isAuthRoute = nextUrl.pathname.startsWith("/auth");
|
||||
|
||||
// Allow API auth routes to pass through
|
||||
if (isApiAuthRoute) {
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// If user is on auth pages and already logged in, redirect to dashboard
|
||||
if (isAuthRoute && isLoggedIn) {
|
||||
return NextResponse.redirect(new URL("/", nextUrl));
|
||||
}
|
||||
|
||||
// If user is not logged in and trying to access protected routes
|
||||
if (!isLoggedIn && !isPublicRoute && !isAuthRoute) {
|
||||
let callbackUrl = nextUrl.pathname;
|
||||
if (nextUrl.search) {
|
||||
callbackUrl += nextUrl.search;
|
||||
}
|
||||
|
||||
const encodedCallbackUrl = encodeURIComponent(callbackUrl);
|
||||
return NextResponse.redirect(
|
||||
new URL(`/auth/signin?callbackUrl=${encodedCallbackUrl}`, nextUrl),
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
});
|
||||
|
||||
// Configure which routes the middleware should run on
|
||||
export const config = {
|
||||
matcher: [
|
||||
/*
|
||||
* Match all request paths except for the ones starting with:
|
||||
* - _next/static (static files)
|
||||
* - _next/image (image optimization files)
|
||||
* - favicon.ico (favicon file)
|
||||
* - public files (images, etc.)
|
||||
*/
|
||||
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user