generated from FOSTI-UMS/FOSTI-Project-Template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware.js
47 lines (42 loc) · 1.52 KB
/
middleware.js
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
import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
export async function middleware(req) {
const base_url = process.env.base_url;
const res = NextResponse.next();
const supabaseMiddleware = createMiddlewareClient({ req, res });
const gmt7TimeZone = 'Asia/Bangkok'; // GMT+7 timezone
const now = new Date().toLocaleString('en-US', { timeZone: gmt7TimeZone });
const sekarang = new Date(now).getTime();
const d = await fetch(base_url + '/api/pendaftaran');
const daftar = await d.json();
const {
data: { user },
} = await supabaseMiddleware.auth.getUser();
// ADMIN CHECKER
if (user) {
const { data: userdata } = await supabaseMiddleware
.from('users')
.select('is_admin')
.eq('email', user.email)
.single();
if (!userdata?.is_admin) {
if (req.nextUrl.pathname == '/admin' || req.nextUrl.pathname == '/profile') {
return NextResponse.redirect(new URL('/', req.url));
}
}
}
else if (!user) {
if (req.nextUrl.pathname == '/admin') {
return NextResponse.redirect(new URL('/admin/#', req.url));
}
else if (req.nextUrl.pathname.startsWith('/admin/#')) {
return res;
}
else if (req.nextUrl.pathname !== '/') {
return NextResponse.redirect(new URL('/', req.url));
}
}
}
export const config = {
matcher: ['/', '/#', '/register', '/profile', '/file-collection', '/admin/:path*', '/register', '/payments'],
};