Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
WebBack
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
websystem
WebBack
Commits
177fc754
Commit
177fc754
authored
3 months ago
by
tpgus2603
Browse files
Options
Downloads
Patches
Plain Diff
refactor: 로그인 리다이렉트 url 프론트로 수정
parent
b82d73cd
No related branches found
No related tags found
1 merge request
!42
[#25] 배포코드 master브랜치로 이동
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
app.js
+1
-1
1 addition, 1 deletion
app.js
middlewares/auth.js
+6
-5
6 additions, 5 deletions
middlewares/auth.js
passport/googleStrategy.js
+4
-4
4 additions, 4 deletions
passport/googleStrategy.js
routes/auth.js
+25
-8
25 additions, 8 deletions
routes/auth.js
with
36 additions
and
18 deletions
app.js
+
1
−
1
View file @
177fc754
...
...
@@ -19,7 +19,7 @@ app.use(morgan('dev')); //로깅용
// CORS 설정
app
.
use
(
cors
({
origin
:
'
http://localhost:3000
'
,
origin
:
process
.
env
.
FRONTEND_URL
,
methods
:
[
'
GET
'
,
'
POST
'
,
'
PUT
'
,
'
DELETE
'
,
'
OPTIONS
'
],
allowedHeaders
:
[
'
Content-Type
'
,
'
Authorization
'
],
credentials
:
true
,
...
...
This diff is collapsed.
Click to expand it.
middlewares/auth.js
+
6
−
5
View file @
177fc754
// middlewares/auth.js
exports
.
isLoggedIn
=
(
req
,
res
,
next
)
=>
{
//로그인된 사용자자만 접근허용
exports
.
isLoggedIn
=
(
req
,
res
,
next
)
=>
{
// 로그인된 사용자만 접근 허용
if
(
req
.
isAuthenticated
())
{
return
next
();
}
res
.
redirect
(
'
/auth/login
'
);
// 리다이렉트 대신 401 Unauthorized 상태 반환
res
.
status
(
401
).
json
({
error
:
'
로그인 되지않은 사용자
'
});
};
exports
.
isNotLoggedIn
=
(
req
,
res
,
next
)
=>
{
//로그인 안
되면 리다이렉트
exports
.
isNotLoggedIn
=
(
req
,
res
,
next
)
=>
{
//
로그인 안
된 사용자만 접근 허용
if
(
!
req
.
isAuthenticated
())
{
return
next
();
}
res
.
redirect
(
'
/
'
);
// 리다이렉트 대신 400 Bad Request 상태 반환 (필요에 따라 변경 가능)
res
.
status
(
400
).
json
({
error
:
'
이미 로그인된
'
});
};
This diff is collapsed.
Click to expand it.
passport/googleStrategy.js
+
4
−
4
View file @
177fc754
// passport/googleStrategy.js
const
{
Strategy
:
GoogleStrategy
}
=
require
(
'
passport-google-oauth20
'
);
const
User
=
require
(
'
../models/user
'
);
const
User
=
require
(
'
../models/user
'
);
// 사용자 모델을 가져옵니다.
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
(
accessToken
,
refreshToken
,
profile
,
done
)
=>
{
async
(
req
,
accessToken
,
refreshToken
,
profile
,
done
)
=>
{
try
{
// 프로필에서 사용자 정보 추출
const
email
=
profile
.
emails
[
0
].
value
;
...
...
@@ -23,7 +23,7 @@ module.exports = new GoogleStrategy(
return
done
(
null
,
user
);
}
catch
(
err
)
{
return
done
(
err
);
return
done
(
err
,
null
);
}
}
);
This diff is collapsed.
Click to expand it.
routes/auth.js
+
25
−
8
View file @
177fc754
// routes/auth.js
const
express
=
require
(
'
express
'
);
const
passport
=
require
(
'
passport
'
);
...
...
@@ -12,23 +11,41 @@ router.get('/login', (req, res) => {
// GET /auth/logout
router
.
get
(
'
/logout
'
,
(
req
,
res
)
=>
{
req
.
logout
(()
=>
{
res
.
redirect
(
'
/
'
);
req
.
logout
((
err
)
=>
{
if
(
err
)
{
return
res
.
status
(
500
).
json
({
error
:
'
Failed to logout
'
});
}
res
.
redirect
(
process
.
env
.
FRONTEND_URL
);
});
});
// GET /auth/google
router
.
get
(
'
/google
'
,
passport
.
authenticate
(
'
google
'
,
{
scope
:
[
'
profile
'
,
'
email
'
]
})
);
router
.
get
(
'
/google
'
,
(
req
,
res
,
next
)
=>
{
const
redirectUrl
=
req
.
query
.
redirectUrl
||
process
.
env
.
FRONTEND_URL
;
// 리다이렉트 URL 검증
const
allowedDomains
=
[
process
.
env
.
FRONTEND_URL
];
if
(
!
allowedDomains
.
some
((
domain
)
=>
redirectUrl
.
startsWith
(
domain
)))
{
return
res
.
status
(
400
).
json
({
error
:
'
Invalid redirect URL
'
});
}
// 세션에 redirectUrl 저장
req
.
session
.
redirectUrl
=
redirectUrl
;
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
)
=>
{
res
.
redirect
(
'
/
'
);
// 세션에서 redirectUrl 가져오기
const
redirectUrl
=
req
.
session
.
redirectUrl
||
process
.
env
.
FRONTEND_URL
;
// 세션에서 redirectUrl 제거
req
.
session
.
redirectUrl
=
null
;
res
.
redirect
(
redirectUrl
);
}
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment