-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmiddleware.basic.ts
More file actions
43 lines (34 loc) · 1.09 KB
/
Copy pathmiddleware.basic.ts
File metadata and controls
43 lines (34 loc) · 1.09 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const publicPaths = ["/login", "/registration"];
const publicApiPaths = ["/api/auth/login"];
function isPublicPath(pathname: string): boolean {
return (
publicPaths.some((p) => pathname === p || pathname.startsWith(`${p}/`)) ||
publicApiPaths.includes(pathname)
);
}
export default function basicMiddleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (isPublicPath(pathname)) {
return NextResponse.next();
}
// FIXME: Comment it out. Not sure what this for.
// const token = request.nextUrl.searchParams.get("token");
// if (token) {
// return NextResponse.next();
// }
const hasSession =
request.cookies.has("ory_kratos_session") ||
request.cookies.has("authorization");
if (!hasSession) {
const returnTo = encodeURIComponent(request.url);
return NextResponse.redirect(
new URL(`/login?return_to=${returnTo}`, request.url)
);
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!.*\\..*|_next).*)"]
};