-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
32 lines (26 loc) · 961 Bytes
/
proxy.ts
File metadata and controls
32 lines (26 loc) · 961 Bytes
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
import { NextRequest, NextResponse } from 'next/server';
import { createSessionManager } from '@/server/auth/createSessionManager';
import {
createCookieHelper,
createCookieStoreFromServer,
} from '@/server/createCookieHelper';
export async function proxy(request: NextRequest) {
const response = NextResponse.next();
const { cookieStore } = await createCookieHelper(
createCookieStoreFromServer(request, response)
);
const { getSession } = await createSessionManager({ cookieStore });
const user = await getSession();
const pathname = request.nextUrl.pathname;
if (pathname === '/') {
if (user) {
return NextResponse.redirect(new URL(`/${user.handle}`, request.url));
}
} else if (pathname.startsWith('/settings') && !user) {
return NextResponse.redirect(new URL('/login', request.url));
}
return response;
}
export const config = {
matcher: ['/((?!_next/static|favicon.ico|_next/image|.*\\.png$).*)'],
};