Skip to content
Snippets Groups Projects
Commit e67e1bf4 authored by tpgus2603's avatar tpgus2603
Browse files

refactor: 로그인 오류수정

parent 177fc754
No related branches found
No related tags found
1 merge request!42[#25] 배포코드 master브랜치로 이동
// routes/auth.js
const express = require('express'); const express = require('express');
const passport = require('passport'); const passport = require('passport');
...@@ -15,17 +14,20 @@ router.get('/logout', (req, res) => { ...@@ -15,17 +14,20 @@ router.get('/logout', (req, res) => {
if (err) { if (err) {
return res.status(500).json({ error: 'Failed to logout' }); return res.status(500).json({ error: 'Failed to logout' });
} }
res.redirect(process.env.FRONTEND_URL); res.redirect(process.env.FRONTEND_URL || 'http://localhost:3000'); // 기본값 설정
}); });
}); });
// GET /auth/google // GET /auth/google
router.get('/google', (req, res, next) => { router.get('/google', (req, res, next) => {
const redirectUrl = req.query.redirectUrl || process.env.FRONTEND_URL; // 기본 redirectUrl 설정
const redirectUrl = req.query.redirectUrl || process.env.FRONTEND_URL || 'http://localhost:3000';
// 리다이렉트 URL 검증 // allowedDomains 배열 확인 및 기본값 설정
const allowedDomains = [process.env.FRONTEND_URL]; const allowedDomains = [process.env.FRONTEND_URL || 'http://localhost:3000'];
if (!allowedDomains.some((domain) => redirectUrl.startsWith(domain))) {
// redirectUrl 검증
if (!allowedDomains.some((domain) => redirectUrl && redirectUrl.startsWith(domain))) {
return res.status(400).json({ error: 'Invalid redirect URL' }); return res.status(400).json({ error: 'Invalid redirect URL' });
} }
...@@ -41,10 +43,12 @@ router.get( ...@@ -41,10 +43,12 @@ router.get(
passport.authenticate('google', { failureRedirect: '/auth/login' }), passport.authenticate('google', { failureRedirect: '/auth/login' }),
(req, res) => { (req, res) => {
// 세션에서 redirectUrl 가져오기 // 세션에서 redirectUrl 가져오기
const redirectUrl = req.session.redirectUrl || process.env.FRONTEND_URL; const redirectUrl = req.session.redirectUrl || process.env.FRONTEND_URL || 'http://localhost:3000';
// 세션에서 redirectUrl 제거 // 세션에서 redirectUrl 제거
req.session.redirectUrl = null; req.session.redirectUrl = null;
// 프론트엔드로 리다이렉트
res.redirect(redirectUrl); res.redirect(redirectUrl);
} }
); );
......
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