forked from juanscr24/tasklancer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
38 lines (31 loc) · 1.1 KB
/
middleware.ts
File metadata and controls
38 lines (31 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import NextAuth from "next-auth"
import authConfig from "./auth.config"
import { NextResponse } from "next/server";
// Create the NextAuth middleware using the provided configuration
const { auth: middleware } = NextAuth(authConfig)
// Rutas publicas
const publicRoutes = [
"/",
"/auth",
"/api/verify-email",
];
// Middleware
export default middleware((req) => {
const { nextUrl, auth } = req;
const isLoggedIn = !!auth?.user;
const isPublicRoute = publicRoutes.includes(nextUrl.pathname);
const isAuthRoute = nextUrl.pathname.startsWith("/auth");
// If user is logged in and trying to access auth pages, redirect to dashboard
if (isLoggedIn && isAuthRoute) {
return NextResponse.redirect(new URL("/dashboard", nextUrl));
}
// If user is not logged in and trying to access protected routes, redirect to login
if (!isLoggedIn && !isPublicRoute) {
return NextResponse.redirect(new URL("/auth", nextUrl));
}
return NextResponse.next();
});
// Define the paths where the middleware should be applied
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};