-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
35 lines (27 loc) · 948 Bytes
/
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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getIronSession } from 'iron-session/edge';
import { sessionOptions } from '@libs/session';
export const middleware = async (req: NextRequest) => {
const res = NextResponse.next();
const session = await getIronSession(req, res, sessionOptions);
// do anything with session here:
const { user } = session;
// like mutate user:
// user.something = someOtherThing;
// or:
// session.user = someoneElse;
// uncomment next line to commit changes:
// await session.save();
// or maybe you want to destroy session:
// await session.destroy();
console.log('user session from middleware:', JSON.stringify(user, null, 2));
// demo:
if (user?.login !== '[email protected]') {
return new NextResponse(null, { status: 403 }); // unauthorized to see pages inside admin/
}
return res;
};
export const config = {
matcher: '/dashboard',
};