Skip to content
Snippets Groups Projects
Commit 813eb395 authored by Ubuntu's avatar Ubuntu
Browse files

refactor: 프록시설정 및 https 쿠키설정

parent 8a447bbd
No related branches found
No related tags found
1 merge request!42[#25] 배포코드 master브랜치로 이동
......@@ -18,32 +18,16 @@ const app = express();
app.use(morgan('dev')); //로깅용
// CORS 설정
const corsOptions = {
origin: (origin, callback) => {
console.log('CORS Origin:', origin); // 디버깅용 로그
const allowedOrigins = [
'http://localhost:3000', // 로컬 개발 환경
'http://ec2-43-203-68-83.ap-northeast-2.compute.amazonaws.com', // EC2 백엔드
];
if (!origin) return callback(null, true); // origin이 없으면 허용 (e.g., Postman)
if (allowedOrigins.includes(origin)) {
return callback(null, true);
}
console.log('CORS origin rejected:', origin); // 차단된 origin 로그
return callback(new Error('Not allowed by CORS'));
},
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true, // 쿠키 허용
};
// CORS 미들웨어 적용
app.use(cors(corsOptions));
// Preflight 요청 처리
app.options('*', cors(corsOptions));
// CORS 설정
app.use(
cors({
origin: 'https://yanawa.shop',
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
})
);
//
app.use(
session({
......@@ -60,7 +44,6 @@ app.use(
})
);
// 미들웨어 설정
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
......@@ -71,6 +54,9 @@ app.use(passport.session());
app.use(flash());
app.set('trust proxy', 1);
console.log('MongoDB URI:', process.env.MONGO_URI);
//라우터 등록
const authRoutes = require('./routes/auth');
......
MongoDB URI: mongodb+srv://admin:lim1234!!@goodmeeting.vkniz.mongodb.net/
(node:4550) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(node:8604) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4550) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(node:8604) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
✅ MongoDB 연결 성공
Rdb데이터베이스 연결 성공.
모든 모델이 성공적으로 동기화되었습니다.
Server is running on 8080
CORS Origin: undefined
GET /api/auth/login 302 7.628 ms - 0
CORS Origin: undefined
GET /api/schedule/all 401 3.639 ms - 44
CORS Origin: undefined
GET /api/schedule/all 401 1.347 ms - 44
......@@ -7,7 +7,6 @@ module.exports = new GoogleStrategy(
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL,
passReqToCallback: true, // req 객체를 콜백에 전달
},
async (req, accessToken, refreshToken, profile, done) => {
try {
......
......@@ -6,13 +6,8 @@ const router = express.Router();
// GET api/auth/login
router.get('/login', (req, res, next) => {
// 프론트엔드에서 전달한 redirectUrl 가져오기
const redirectUrl = req.query.redirectUrl || process.env.FRONTEND_URL || 'http://localhost:3000';
// redirectUrl 유효성 검증
const allowedDomains = [process.env.FRONTEND_URL || 'http://localhost:3000'];
if (!allowedDomains.some((domain) => redirectUrl.startsWith(domain))) {
return res.status(400).json({ error: 'Invalid redirect URL' });
}
const redirectUrl = req.query.redirectUrl || process.env.FRONTEND_URL || 'https://yanawa.shop';
// redirectUrl 유효성 검증
// redirectUrl 세션에 저장
req.session.redirectUrl = redirectUrl;
......@@ -20,20 +15,23 @@ router.get('/login', (req, res, next) => {
// Google OAuth 인증 시작
passport.authenticate('google', { scope: ['profile', 'email'] })(req, res, next);
});
// GET /auth/google/callback
router.get(
'/google/callback',
passport.authenticate('google', { failureRedirect: '/auth/login' }),
(req, res) => {
// 세션에서 redirectUrl 가져오기
const redirectUrl = req.session.redirectUrl || process.env.FRONTEND_URL || 'http://localhost:3000';
const redirectUrl = req.session.redirectUrl || 'https://yanawa.shop';
// 세션에서 redirectUrl 제거
req.session.redirectUrl = null;
// 인증 완료 후 프론트엔드로 리다이렉트
res.redirect(redirectUrl);
req.session.save((err) => {
if (err) {
console.error('세션 저장 오류:', err);
return res.status(500).json({ error: '서버 오류' });
}
res.redirect(redirectUrl);
});
}
);
......
(node:4551) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(node:8605) [MONGODB DRIVER] Warning: useNewUrlParser is a deprecated option: useNewUrlParser has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4551) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
(node:8605) [MONGODB DRIVER] Warning: useUnifiedTopology is a deprecated option: useUnifiedTopology has no effect since Node.js Driver version 4.0.0 and will be removed in the next major version
MongoDB 연결 실패: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
at _handleConnectionErrors (/home/ubuntu/webback/node_modules/mongoose/lib/connection.js:909:11)
at NativeConnection.openUri (/home/ubuntu/webback/node_modules/mongoose/lib/connection.js:860:11)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment