27 lines
637 B
TypeScript
27 lines
637 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { auth } from "./auth";
|
|
|
|
export default auth((req) => {
|
|
const { nextUrl } = req;
|
|
const isLoggedIn = !!req.auth;
|
|
const isLoginPage = nextUrl.pathname === "/login";
|
|
|
|
if (!isLoggedIn && !isLoginPage) {
|
|
const url = nextUrl.clone();
|
|
url.pathname = "/login";
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
if (isLoggedIn && isLoginPage) {
|
|
const url = nextUrl.clone();
|
|
url.pathname = "/dashboard";
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
});
|
|
|
|
export const config = {
|
|
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
|
|
};
|