forked from aprnna/KomRest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
56 lines (45 loc) · 1.88 KB
/
middleware.ts
File metadata and controls
56 lines (45 loc) · 1.88 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
44
45
46
47
48
49
50
51
52
53
54
55
56
import { type NextRequest, NextResponse } from 'next/server'
import { updateSession } from '@/utils/supabase/middleware'
import { createClient } from './utils/supabase/server';
import { redirect } from 'next/navigation';
export async function middleware(req: NextRequest) {
await updateSession(req)
const { pathname } = req.nextUrl;
// Allow requests to /api and /login to pass through
const allowedPaths = ['/error/unauthorized','/error'];
if (pathname == '/') return NextResponse.redirect(new URL('/auth/login', req.url));
if (pathname.startsWith('/api') || pathname.startsWith('/auth') || allowedPaths.includes(pathname)) {
return NextResponse.next();
}
const supabase = createClient();
const {data:{user}, error:errorAuth} = await supabase.auth.getUser()
if (errorAuth ) return NextResponse.redirect(new URL('/auth/login', req.url));
const { data, error} = await supabase.from('users').select('role').eq('id', user?.id).single()
const role = data?.role
if (error) console.error(error)
const roleAccess: { [key: string]: string[] } = {
manager: ['/admin','/admin/karyawan'],
koki: ['/menu','/pesanan/ongoing'],
karyawan: ['/bahan_baku','/bahan_baku/riwayat'],
pelayan: ['/reservasi','/pesanan/add', '/pesanan']
};
if(role){
const allowedRoutes = roleAccess[role] || [];
if (!allowedRoutes || !allowedRoutes.includes(pathname)) {
return NextResponse.redirect(new URL('/error/unauthorized', req.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* Feel free to modify this pattern to include more paths.
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
}