Choorai
| 문서
보안 관련 주의 필요

인증/쿠키 문제 해결하기

로그인했는데 새로고침하면 로그아웃되나요? 쿠키 설정이나 CORS 관련 문제일 수 있습니다.

TL;DR (핵심 요약)

1) credentials: 'include' 설정 2) 서버의 CORS에서 allow_credentials=True 3) 쿠키에 SameSite=None; Secure 속성 추가

주의

쿠키가 저장되지 않음 / 로그인이 유지되지 않음

원인

프론트엔드와 백엔드가 다른 도메인에 있을 때 쿠키 전송을 위한 설정이 누락되었습니다.

해결책
  1. 프론트엔드: fetch/axios에서 credentials: 'include' 설정
  2. 백엔드: CORS에서 allow_credentials=True, allow_origins에 정확한 도메인 지정
  3. 쿠키: SameSite=None, Secure=True 설정 (HTTPS 필수)

프론트엔드 설정

Fetch API

api.js
// 쿠키를 포함해서 요청 보내기
fetch('https://api.example.com/login', {
  method: 'POST',
  credentials: 'include',  // 중요!
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ email, password }),
});

Axios

api.js
import axios from 'axios';

// 전역 설정
axios.defaults.withCredentials = true;

// 또는 개별 요청에서
axios.post('https://api.example.com/login', data, {
  withCredentials: true  // 중요!
});

백엔드 설정

Python FastAPI

main.py
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://your-frontend.com"],  # * 사용 불가!
    allow_credentials=True,  # 중요!
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/login")
async def login(response: Response):
    # 쿠키 설정
    response.set_cookie(
        key="session",
        value="token-value",
        httponly=True,
        secure=True,        # HTTPS 필수
        samesite="none",    # 크로스 사이트 허용
        max_age=3600 * 24 * 7  # 7일
    )
    return {"message": "로그인 성공"}

Node.js Express

server.js
const express = require('express');
const cors = require('cors');

const app = express();

app.use(cors({
  origin: 'https://your-frontend.com',
  credentials: true  // 중요!
}));

app.post('/login', (req, res) => {
  res.cookie('session', 'token-value', {
    httpOnly: true,
    secure: true,
    sameSite: 'none',
    maxAge: 7 * 24 * 60 * 60 * 1000  // 7일
  });
  res.json({ message: '로그인 성공' });
});

HTTPS 필수!

SameSite=NoneSecure=True와 함께 사용해야 합니다. 즉, HTTPS 환경에서만 동작합니다. 개발 중에는 localhost가 예외적으로 허용됩니다.

절대 하지 마세요!

  • allow_origins=["*"]allow_credentials=True를 함께 사용
  • 민감한 토큰을 localStorage에 저장
  • httpOnly 없이 세션 쿠키 설정

디버깅 방법

  1. 브라우저 개발자 도구(F12) → Application → Cookies 확인
  2. Network 탭에서 요청 헤더에 Cookie가 포함되는지 확인
  3. 응답 헤더에 Set-Cookie가 있는지 확인
  4. Console에 CORS 관련 에러가 없는지 확인

관련 문서