import { withAuth } from "next-auth/middleware" import { NextResponse } from "next/server"; // middleware is applied to all routes, use conditionals to select export default withAuth(function middleware(req) { const { pathname } = req.nextUrl; // Definir rutas públicas const publicRoutes = [ '/api/callback' ]; // Verificar si la ruta es pública if (publicRoutes.includes(pathname)) { return NextResponse.next(); } }, { callbacks: { authorized: async ({ req, token }) => { const res = await fetch(`${process.env.NEXT_URL_BASE}/api/ping`); const isPausePage = req.nextUrl.pathname === '/pause' const isPauseSession = req.cookies.get('pause_session') console.log(isPauseSession, isPausePage, token); console.log("Anuar"); try { let data = await res.json(); if (data.status === false) { return false } } catch (error) { } // Si hay sesión pausada y no está en la página de pausa if (isPauseSession && !isPausePage && !token) { return NextResponse.redirect(new URL('/pause', req.url)) } if (req.nextUrl.pathname.startsWith("/messenger") && token === null) { return false } if (req.nextUrl.pathname.startsWith("/chat/select") && token === null) { return false } if (req.nextUrl.pathname.startsWith("/ping") && token === null) { return false } return true }, }, pages: { signIn: "/chat/login", } }) export const config = { matcher: [ '/messenger', '/chat/select', '/pause', '/auth/callback', ] }