-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
37 lines (30 loc) · 1002 Bytes
/
middleware.ts
File metadata and controls
37 lines (30 loc) · 1002 Bytes
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
import { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const { pathname } = req.nextUrl;
if (
pathname === "/" ||
pathname.startsWith("/auth/login") ||
pathname.startsWith("/auth/register") ||
pathname === "/auth/newUser" ||
pathname.startsWith("/api/auth") ||
pathname.startsWith("/_next")
) {
return NextResponse.next();
}
if (req.auth?.user && !req.auth.user.username) {
console.log("Redirecting to new user page because username is not set");
const newUrl = new URL("/auth/newUser", req.nextUrl.origin);
return NextResponse.redirect(newUrl);
}
if (req.auth?.user) {
return NextResponse.next();
}
const newUrl = new URL("/", req.nextUrl.origin);
return NextResponse.redirect(newUrl);
});
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};
// NOTE: add export const runtime = nodejs while deploying
export const runtime = "nodejs";