(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push(["chunks/[root-of-the-server]__13jvavz._.js",
"[externals]/node:buffer [external] (node:buffer, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("node:buffer", () => require("node:buffer"));

module.exports = mod;
}),
"[externals]/node:async_hooks [external] (node:async_hooks, cjs)", ((__turbopack_context__, module, exports) => {

const mod = __turbopack_context__.x("node:async_hooks", () => require("node:async_hooks"));

module.exports = mod;
}),
"[project]/src/middleware.ts [middleware-edge] (ecmascript)", ((__turbopack_context__) => {
"use strict";

__turbopack_context__.s([
    "config",
    ()=>config,
    "middleware",
    ()=>middleware
]);
var __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$api$2f$server$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__$3c$locals$3e$__ = __turbopack_context__.i("[project]/node_modules/next/dist/esm/api/server.js [middleware-edge] (ecmascript) <locals>");
var __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$server$2f$web$2f$spec$2d$extension$2f$response$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__ = __turbopack_context__.i("[project]/node_modules/next/dist/esm/server/web/spec-extension/response.js [middleware-edge] (ecmascript)");
;
function base64UrlDecode(value) {
    let base64 = value.replace(/-/g, '+').replace(/_/g, '/');
    while(base64.length % 4 !== 0){
        base64 += '=';
    }
    try {
        return atob(base64);
    } catch  {
        return '';
    }
}
function parseJwt(token) {
    const parts = token.split('.');
    if (parts.length < 2) return null;
    try {
        return JSON.parse(base64UrlDecode(parts[1]));
    } catch  {
        return null;
    }
}
function isTokenExpired(token) {
    const payload = parseJwt(token);
    if (!payload || typeof payload.exp !== 'number') return true;
    return Math.floor(Date.now() / 1000) >= payload.exp;
}
function middleware(request) {
    const { pathname } = request.nextUrl;
    const token = request.cookies.get('token')?.value;
    // const { clienttoken } = useAuthContext();
    const clienttoken = request.cookies.get('clienttoken')?.value;
    const expired = token ? isTokenExpired(token) : true;
    const clientExpired = clienttoken ? isTokenExpired(clienttoken) : true;
    const isAdminRoute = pathname.startsWith('/admin');
    const isLoginPage = pathname === '/admin/login';
    const isAuthPages = pathname === "/login" || pathname === "/register";
    const isDashboardRoute = pathname === '/dashboard' || pathname.startsWith('/dashboard/');
    const isCustomerRoute = pathname.startsWith('/profile') || pathname.startsWith('/orders') || pathname.startsWith('/cart') || pathname.startsWith('/products');
    /**
   * DASHBOARD PROTECTION
   */ if (isDashboardRoute && (!clienttoken || clientExpired)) {
        const loginUrl = request.nextUrl.clone();
        loginUrl.pathname = '/login';
        loginUrl.searchParams.set('next', pathname);
        return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$server$2f$web$2f$spec$2d$extension$2f$response$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__["NextResponse"].redirect(loginUrl);
    }
    /**
   * CUSTOMER PROTECTION
   */ if (isCustomerRoute && (!clienttoken || clientExpired)) {
        return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$server$2f$web$2f$spec$2d$extension$2f$response$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__["NextResponse"].redirect(new URL('/login', request.url));
    }
    /**
   * ADMIN
   */ if (isAdminRoute && !isLoginPage && (!token || expired)) {
        const response = __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$server$2f$web$2f$spec$2d$extension$2f$response$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__["NextResponse"].redirect(new URL('/admin/login', request.url));
        response.cookies.set('token', '', {
            path: '/',
            expires: new Date(0)
        });
        return response;
    }
    if (isLoginPage && token && !expired) {
        return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$server$2f$web$2f$spec$2d$extension$2f$response$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__["NextResponse"].redirect(new URL('/admin/', request.url));
    }
    /**
   * AUTH PAGES BLOCK IF LOGGED IN
   */ if (isAuthPages && clienttoken && !clientExpired) {
        const redirectUrl = request.nextUrl.clone();
        const nextPage = request.nextUrl.searchParams.get('next');
        redirectUrl.pathname = nextPage && nextPage.startsWith('/') ? nextPage : '/dashboard';
        redirectUrl.search = '';
        return __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$server$2f$web$2f$spec$2d$extension$2f$response$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__["NextResponse"].redirect(redirectUrl);
    }
    const response = __TURBOPACK__imported__module__$5b$project$5d2f$node_modules$2f$next$2f$dist$2f$esm$2f$server$2f$web$2f$spec$2d$extension$2f$response$2e$js__$5b$middleware$2d$edge$5d$__$28$ecmascript$29$__["NextResponse"].next();
    response.headers.set("url-pathname", pathname);
    return response;
}
const config = {
    matcher: [
        '/',
        '/admin/:path*',
        '/profile/:path*',
        '/orders/:path*',
        '/wishlist/:path*',
        '/dashboard',
        '/dashboard/:path*',
        '/login',
        '/register'
    ]
};
}),
]);

//# sourceMappingURL=%5Broot-of-the-server%5D__13jvavz._.js.map