-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
67 lines (58 loc) · 1.75 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { NextResponse, type NextFetchEvent } from 'next/server';
import { withAuth, type NextRequestWithAuth } from 'next-auth/middleware';
// export const config = {
// // matcher: ["/profile"],
// matcher: ['/((?!register|api|login).*)'],
// };
// This function can be marked `async` if using `await` inside
// export function middleware(request: NextRequest, fetchEvent: NextFetchEvent) {
// console.log(request);
// // return NextResponse.next({
// // request: {
// // test: 123,
// // },
// // })
// if (request.nextUrl.pathname.startsWith('/api')) {
// // return NextResponse.rewrite(new URL('/about-2', request.url))
// // return response;
// }
// }
export default withAuth(
async (request: NextRequestWithAuth, fetchEvent: NextFetchEvent) => {
const { nextauth } = request;
console.log('nextauth', nextauth, fetchEvent);
// return NextResponse.json({
// message: 'ok',
// }, {
// status: 402,
// });
return NextResponse.next({
// headers: {
// nextauth,
// },
});
},
{
callbacks: {
async authorized({ req, token }) {
const { cookies } = req;
console.log('callbacks token', token);
// 从数据库获取session信息
// const session = await getSession({
// req: {
// headers: {
// cookie: cookies.toString(),
// },
// },
// });
const sessionToken = cookies.get('next-auth.session-token');
return !!sessionToken;
},
},
},
);
export const config = {
// https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
// matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
matcher: ['/dashboard/:path*'],
};