-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
36 lines (30 loc) · 1.11 KB
/
middleware.ts
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
import { cookies } from "next/headers"
import type { NextRequest } from "next/server"
import { SessionOptions, getIronSession } from "iron-session"
import { env } from "./lib/env.mjs"
export interface SessionData {
email: string
isLoggedIn: boolean
}
export const sessionOptions: SessionOptions = {
cookieName: "next-starter-auth",
password: env.AUTH_SECRET,
cookieOptions: {
// secure only works in `https` environments
// if your localhost is not on `https`, then use: `secure: process.env.NODE_ENV === "production"`
secure: env.NODE_ENV === "production",
},
}
export const getSession = () =>
getIronSession<SessionData>(cookies(), sessionOptions)
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const session = await getIronSession<SessionData>(cookies(), sessionOptions)
if (!session.isLoggedIn) {
return Response.redirect(`${request.nextUrl.origin}/login`, 302)
}
}
// See "Matching Paths" below to learn more
export const config = {
matcher: "/((?!api|_next/static|_next/image|favicon.ico|login|verify).*)",
}