-
Notifications
You must be signed in to change notification settings - Fork 34
/
middleware.ts
44 lines (35 loc) · 1.17 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
37
38
39
40
41
42
43
44
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import micromatch from "micromatch";
import { supabase } from "@/modules/common/server/supabase";
// Add API routes that don't require authentication
const unAuthenticatedApiRoutes = [
"/api/auth/signin",
"/api/auth/signup",
"/api/invites/*/accept",
"/api/invites/*/decline",
];
export async function middleware(req: NextRequest) {
const { headers, nextUrl } = req;
const { pathname } = nextUrl;
// Bypass routes that don't require authentication
if (micromatch.isMatch(pathname, unAuthenticatedApiRoutes)) {
return NextResponse.next();
}
// Authenticate the request
const token = headers.get("authorization")?.replace("Bearer ", "");
const { error } = await supabase.auth.getUser(token);
// Return an error if the request is not authenticated
if (error) {
const response = JSON.stringify({ error });
return new NextResponse(response, {
status: 401,
headers: { "content-type": "application/json" },
});
}
return NextResponse.next();
}
// Limit the middleware to specific '/api/*' routes
export const config = {
matcher: ["/api/:path*"],
};