-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.ts
41 lines (33 loc) · 1.05 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
import { withAuth } from 'next-auth/middleware';
import { getToken } from 'next-auth/jwt';
import { PAGES } from '~/lib/constants';
export default withAuth(
async function middleware(req) {
const token = await getToken({ req });
const isAuth = !!token;
const isAuthPage =
req.nextUrl.pathname === PAGES.ROOT ||
req.nextUrl.pathname === PAGES.SIGN_IN ||
req.nextUrl.pathname === PAGES.SIGN_UP;
if (isAuth) {
if (isAuthPage) {
return Response.redirect(new URL(`/${token.username}`, req.url));
}
// if (token.username !== req.nextUrl.pathname.slice(1)) {
// return Response.redirect(new URL(`/error`, req.url));
// }
return null; // just does nothing -> interrupting middleware further exec. 🤷♂️
}
},
{
callbacks: {
async authorized() {
return true; // without it the code above doesn't work
}
}
}
);
// It seems not to be working tbh if I have middleware above like above 🧐
// export const config = {
// matcher: ['/:username*']
// };