-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
35 lines (29 loc) · 1.16 KB
/
proxy.ts
File metadata and controls
35 lines (29 loc) · 1.16 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
// middleware.ts
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export async function proxy(req: NextRequest) {
const res = NextResponse.next();
const supabase = createMiddlewareClient({ req, res });
const { data: { session } } = await supabase.auth.getSession();
if (req.nextUrl.pathname.startsWith('/dashboard')) {
if (!session) {
return NextResponse.redirect(new URL('/login', req.url));
}
// Check admin status - use maybeSingle() to handle missing rows
const { data: userStats, error } = await supabase
.from('user_stats')
.select('is_admin')
.eq('user_id', session.user.id)
.maybeSingle();
// If no row exists, error occurred, or user is not admin -> redirect
if (error || !userStats || !userStats.is_admin) {
return NextResponse.redirect(new URL('/', req.url));
}
}
return res;
}
// Ensure the middleware is only called for relevant paths.
export const config = {
matcher: ['/dashboard/:path*'],
};