-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.ts
40 lines (33 loc) · 1.34 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
39
40
import { NextRequest, NextResponse } from 'next/server';
import { getIronSession } from 'iron-session';
import sessionConfig from './src/utils/session';
import { IronSessionWithOdoo } from '@/types';
export async function middleware(req: NextRequest, res:NextResponse) {
try {
/* On Every Route */
if (
req.nextUrl.pathname.startsWith('/api') ||
req.nextUrl.pathname.startsWith('/dashboard')
) {
//return await fetchUserMiddleware(request)
const session : IronSessionWithOdoo = await getIronSession(req, res, sessionConfig);
console.log("[middleware]session", session)
if(!session) return new NextResponse( JSON.stringify({ status: 403, message:"Forbidden"}))
// Attach the user and partner information to the response for middleware on the server
// res.locals = {
// ...res.locals,
// session: {
// ...session,
// }
// };
return NextResponse.next();
}
/* Redirect example */
// if (request.nextUrl.pathname.startsWith('/dashboard')) {
// return NextResponse.rewrite(new URL('/dashboard/user', request.url))
// }
} catch(e) {
console.error(e);
return new NextResponse(JSON.stringify({ status: 500, text: 'Internal Server Error', error:e }));
}
}