-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
47 lines (36 loc) · 1.38 KB
/
Copy pathmiddleware.ts
File metadata and controls
47 lines (36 loc) · 1.38 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
39
40
41
42
43
44
45
46
47
;
// middleware.ts
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { parse } from "cookie"
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const previewMode = request.nextUrl.searchParams.get("preview") === "true"
console.log(`Middleware invoked for path: ${pathname}`)
// Define paths that are public and do not require authentication
const publicPaths = ["/login", "/favicon.ico", "/_next/", "/static/", "/public"]
if (previewMode) {
console.log("Preview mode detected. Allowing access.")
return NextResponse.next()
}
if (publicPaths.some((path) => pathname.startsWith(path))) {
console.log(`Allowing public path: ${pathname}`)
return NextResponse.next()
}
// Check authentication for other paths
const cookieHeader = request.headers.get("cookie") || ""
const cookies = parse(cookieHeader)
const accessToken = cookies.instagram_access_token
console.log(`Access Token: ${accessToken}`)
if (!accessToken) {
const loginUrl = new URL("/login", request.url)
console.log(`Redirecting to login: ${loginUrl}`)
return NextResponse.redirect(loginUrl)
}
console.log(`Authenticated access to: ${pathname}`)
return NextResponse.next()
}
// Apply middleware to the /uploader route and its subpaths
export const config = {
matcher: ["/uploader/:path*"],
}