-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmiddleware.ts
38 lines (33 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
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { validateAccessCredentials } from '@/app/actions/login';
import { getSession } from '@/app/actions/session';
import { Roles } from '@osu-tournament-rating/otr-api-client';
export async function middleware(req: NextRequest) {
const res = NextResponse.next();
// Validate access credentials if possible
await validateAccessCredentials({ req, res });
const session = await getSession({ req, res });
// Redirect users that aren't logged in
if (!session.isLogged || !session.user?.scopes?.includes(Roles.Whitelist)) {
// Pass through the existing response headers in case cookies are set
return NextResponse.redirect(new URL('/unauthorized', req.url), {
headers: res.headers,
});
}
return res;
}
export const config = {
matcher: [
/*
* Match all paths except:
* - '/api/*' API routes
* - '/auth'
* - '/unauthorized' Has its own access control
* - '/_next/*' Next.js internals
* - '/static/*' Static assets
* - '/favicon.ico' Static assets
*/
'/((?!api|auth|unauthorized|_next|static|favicon.ico).*)',
],
};