Toggle navigation
Toggle navigation
This project
Loading...
Sign in
이승윤
/
OpenSource-MyCookBook
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
이승윤
2021-06-06 18:57:17 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
97560c9b6551a910e618a61810c121ebb875167d
97560c9b
1 parent
c38807a7
feat: facebook login -> google login
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
42 deletions
routes/auth.js
views/accounts/join.ejs
views/accounts/login.ejs
routes/auth.js
View file @
97560c9
...
...
@@ -3,6 +3,7 @@ var router = express.Router();
var
UserModel
=
require
(
'../models/UserModel'
);
var
passport
=
require
(
'passport'
);
var
FacebookStrategy
=
require
(
'passport-facebook'
).
Strategy
;
const
GoogleStrategy
=
require
(
'passport-google-oauth20'
).
Strategy
;
passport
.
serializeUser
(
function
(
user
,
done
)
{
done
(
null
,
user
);
...
...
@@ -12,60 +13,60 @@ passport.deserializeUser(function (user, done) {
done
(
null
,
user
);
});
passport
.
use
(
new
FacebookStrategy
({
// https://developers.facebook.com에서 appId 및 scretID 발급
clientID
:
"863352300499259"
,
//입력하세요
clientSecret
:
"36867723fbdd49dac987f9a061e2206a"
,
//입력하세요.
callbackURL
:
"http://localhost:3000/auth/facebook/callback"
,
profileFields
:
[
'id'
,
'displayName'
,
'photos'
,
'email'
]
//받고 싶은 필드 나열
passport
.
use
(
new
GoogleStrategy
(
{
clientID
:
'912554148550-gq86jjjgc022b1eit50mboh5lq48covi.apps.googleusercontent.com'
,
clientSecret
:
'_EzuXeN7eNCTbcGQUV4kY1pN'
,
callbackURL
:
'http://localhost:3000/auth/google/callback'
,
profileFields
:
[
'id'
,
'displayName'
,
'photos'
,
'email'
],
//받고 싶은 필드 나열
},
function
(
accessToken
,
refreshToken
,
profile
,
done
)
{
//아래 하나씩 찍어보면서 데이터를 참고해주세요.
//console.log(profile);
//console.log(profile.displayName);
//console.log(profile.emails[0].value);
//console.log(profile._raw);
//console.log(profile._json);
UserModel
.
findOne
({
username
:
"fb_"
+
profile
.
id
},
function
(
err
,
user
){
if
(
!
user
){
//없으면 회원가입 후 로그인 성공페이지 이동
var
regData
=
{
//DB에 등록 및 세션에 등록될 데이터
username
:
"fb_"
+
profile
.
id
,
password
:
"facebook_login"
,
displayname
:
profile
.
displayName
function
(
accessToken
,
refreshToken
,
profile
,
done
)
{
UserModel
.
findOne
(
{
username
:
'goo_'
+
profile
.
id
},
function
(
err
,
user
)
{
if
(
!
user
)
{
//없으면 회원가입 후 로그인 성공페이지 이동
var
regData
=
{
//DB에 등록 및 세션에 등록될 데이터
username
:
'goo_'
+
profile
.
id
,
password
:
'google_login'
,
displayname
:
profile
.
displayName
,
};
var
User
=
new
UserModel
(
regData
);
User
.
save
(
function
(
err
){
//DB저장
done
(
null
,
regData
);
//세션 등록
User
.
save
(
function
(
err
)
{
//DB저장
done
(
null
,
regData
);
//세션 등록
});
}
else
{
//있으면 DB에서 가져와서 세션등록
done
(
null
,
user
);
}
else
{
//있으면 DB에서 가져와서 세션등록
done
(
null
,
user
);
}
});
}
));
);
}
)
);
// http://localhost:3000/auth/facebook 접근시 facebook으로 넘길 url 작성해줌
router
.
get
(
'/facebook'
,
passport
.
authenticate
(
'facebook'
,
{
scope
:
'email'
})
);
router
.
get
(
'/google'
,
passport
.
authenticate
(
'google'
,
{
scope
:
[
'profile'
]
}));
//인증후 페이스북에서 이 주소로 리턴해줌. 상단에 적은 callbackURL과 일치
router
.
get
(
'/facebook/callback'
,
passport
.
authenticate
(
'facebook'
,
{
router
.
get
(
'/google/callback'
,
passport
.
authenticate
(
'google'
,
{
failureRedirect
:
'/auth'
,
successRedirect
:
'/'
,
failureRedirect
:
'/auth/facebook/fail'
}
)
})
);
//로그인 성공시 이동할 주소
router
.
get
(
'/
facebook/success'
,
function
(
req
,
res
)
{
router
.
get
(
'/
google/success'
,
function
(
req
,
res
)
{
res
.
send
(
req
.
user
);
});
router
.
get
(
'/
facebook/fail'
,
function
(
req
,
res
)
{
res
.
send
(
'facebook
login fail'
);
router
.
get
(
'/
google/fail'
,
function
(
req
,
res
)
{
res
.
send
(
'google
login fail'
);
});
module
.
exports
=
router
;
...
...
views/accounts/join.ejs
View file @
97560c9
...
...
@@ -23,8 +23,8 @@
<!-- Change this to a button or input when using this as a form -->
<div class="d-grid gap-2 col-11 mx-auto p-2">
<input type="submit" class="btn btn-lg btn-success btn-block" value="가입하기">
<a href="/auth/
facebook" class="btn btn-lg btn-primary
btn-block">
<i class="fa fa-
facebook" aria-hidden="true"></i> 페이스북
회원가입
<a href="/auth/
google" class="btn btn-lg btn-danger
btn-block">
<i class="fa fa-
google" aria-hidden="true"></i> Google
회원가입
</a>
</div>
</fieldset>
...
...
views/accounts/login.ejs
View file @
97560c9
...
...
@@ -22,8 +22,8 @@
<!-- Change this to a button or input when using this as a form -->
<div class="d-grid gap-2 col-11 mx-auto p-2">
<input type="submit" class="btn btn-lg btn-success btn-block" value="로그인">
<a href="/auth/
facebook" class="btn btn-lg btn-primary
btn-block">
<i class="fa fa-
facebook" aria-hidden="true"></i> 페이스북
로그인
<a href="/auth/
google" class="btn btn-lg btn-danger
btn-block">
<i class="fa fa-
google" aria-hidden="true"></i> Google
로그인
</a>
</div>
</fieldset>
...
...
Please
register
or
login
to post a comment