Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/api/service/auth-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
export const authServiceRemote = () => ({
// 로그인
login: async (payload: LoginRequest) => {
const data = await api.post<LoginResponse>('/auth/login', payload);
const data = await api.post<LoginResponse>('/auth/login', payload, { withCredentials: true });

setAccessToken(data.accessToken, data.expiresIn);
return data;
Expand All @@ -22,7 +22,7 @@ export const authServiceRemote = () => ({

// 로그아웃
logout: async () => {
await api.post<void>('/auth/logout');
await api.post<void>('/auth/logout', null, { withCredentials: true });
clearAccessToken();
},

Expand All @@ -40,7 +40,7 @@ export const authServiceRemote = () => ({

// 회원 탈퇴
withdraw: async () => {
await api.delete<void>('/auth/withdraw');
await api.delete<void>('/auth/withdraw', { withCredentials: true });
clearAccessToken();
},
});
6 changes: 5 additions & 1 deletion src/lib/auth/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ const ACCESS_TOKEN_KEY = 'accessToken';
export const setAccessToken = (token: string, maxAgeSeconds?: number) => {
if (typeof document === 'undefined') return;

const parts = [`${ACCESS_TOKEN_KEY}=${encodeURIComponent(token)}`, 'path=/'];
const parts = [
`${ACCESS_TOKEN_KEY}=${encodeURIComponent(token)}`,
'path=/',
'domain=.wego.monster',
];

if (typeof maxAgeSeconds === 'number' && maxAgeSeconds > 0) {
parts.push(`Max-Age=${maxAgeSeconds}`);
Expand Down
28 changes: 28 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { NextRequest, NextResponse } from 'next/server';

export default async function proxy(request: NextRequest) {
const accessToken = request.cookies.get('accessToken');
const refreshToken = request.cookies.get('refreshToken');

const protectedPaths = ['/mypage', '/post-meetup', '/message', '/schedule'];
const isProtected = protectedPaths.some((path) => request.nextUrl.pathname.startsWith(path));

// 보호되지 않은 경로는 그냥 통과
if (!isProtected) {
return NextResponse.next();
}

// 둘 다 없으면 로그인 페이지로 redirect
if (!accessToken && !refreshToken) {
const loginUrl = new URL('/login', request.url);
loginUrl.searchParams.set('error', 'unauthorized');
loginUrl.searchParams.set('path', request.nextUrl.pathname);
return NextResponse.redirect(loginUrl);
}

return NextResponse.next();
}

export const config = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 경로들은 위의 프록시에서 제외되는 건가요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 맞아용
내장 next api,정적 자원 요청(이미지 등), 로그인, 회원가입 페이지는 proxy 동작이 필요없기 때문에 제외했습니다~

matcher: ['/((?!api|_next/static|_next/image|favicon.ico|login|signup).*)'],
};