Toggle navigation
Toggle navigation
This project
Loading...
Sign in
MotherProject
/
myYoutube
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
Flare-k
2020-06-16 23:18:55 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
dc8b2cb4063242707c7a9e9de678a95161ee4034
dc8b2cb4
1 parent
0e7e8990
[Authenticate] Facebook Login
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
65 additions
and
6 deletions
controllers/userController.js
package.json
passport.js
routers/globalRouter.js
routes.js
views/partials/socialLogin.pug
controllers/userController.js
View file @
dc8b2cb
...
...
@@ -78,6 +78,20 @@ export const postGithubLogin = (req, res) => {
res
.
redirect
(
routes
.
home
);
};
export
const
facebookLogin
=
passport
.
authenticate
(
"facebook"
);
export
const
facebookLoginCallback
=
(
accessToken
,
refreshToken
,
profile
,
cb
)
=>
{
console
.
log
(
accessToken
,
refreshToken
,
profile
,
cb
);
};
export
const
postFacebookLogin
=
(
req
,
res
)
=>
{
res
.
redirect
(
routes
.
home
);
};
// 로그아웃을 클릭하면 LogOut페이지로 가는 것 대신에, 로그아웃을 처리한 후
// home 페이지로 Redirect로 표현할 것이다.
// 즉, 초반에 만들어둔 logout.pug는 삭제해도 좋다.
...
...
@@ -91,8 +105,18 @@ export const getMe = (req, res) =>
res
.
render
(
"userDetail"
,
{
pageTitle
:
"User Detail"
,
user
:
req
.
user
});
// export const users = (req, res) => res.render("users", { pageTitle: "Users" });
export
const
userDetail
=
(
req
,
res
)
=>
res
.
render
(
"userDetail"
,
{
pageTitle
:
"User Detail"
});
export
const
userDetail
=
async
(
req
,
res
)
=>
{
const
{
params
:
{
id
},
}
=
req
;
// req로 부터 params의 id가져오기
try
{
const
user
=
await
User
.
findById
(
id
);
res
.
render
(
"userDetail"
,
{
pageTitle
:
"User Detail"
,
user
});
}
catch
(
error
)
{
res
.
redirect
(
routes
.
home
);
}
};
export
const
editProfile
=
(
req
,
res
)
=>
res
.
render
(
"editProfile"
,
{
pageTitle
:
"Edit Profile"
});
export
const
changePassword
=
(
req
,
res
)
=>
...
...
package.json
View file @
dc8b2cb
...
...
@@ -35,6 +35,7 @@
"multer"
:
"^1.4.2"
,
"node-sass"
:
"^4.14.1"
,
"passport"
:
"^0.4.1"
,
"passport-facebook"
:
"^3.0.0"
,
"passport-github"
:
"^1.1.0"
,
"passport-local"
:
"^1.0.0"
,
"passport-local-mongoose"
:
"^6.0.1"
,
...
...
passport.js
View file @
dc8b2cb
import
dotenv
from
"dotenv"
;
import
passport
from
"passport"
;
import
GithubStrategy
from
"passport-github"
;
import
FacebookStrategy
from
"passport-facebook"
;
import
User
from
"./models/User"
;
import
{
githubLoginCallback
}
from
"./controllers/userController"
;
import
{
githubLoginCallback
,
facebookLoginCallback
,
}
from
"./controllers/userController"
;
import
routes
from
"./routes"
;
dotenv
.
config
();
...
...
@@ -21,5 +25,16 @@ passport.use(
)
);
passport
.
use
(
new
FacebookStrategy
(
{
clientID
:
process
.
env
.
FB_ID
,
clientSecret
:
process
.
env
.
FB_SECRET
,
callbackURL
:
`http://localhost:80
${
routes
.
facebookCallback
}
`
,
},
facebookLoginCallback
)
);
passport
.
serializeUser
(
User
.
serializeUser
());
passport
.
deserializeUser
(
User
.
deserializeUser
());
...
...
routers/globalRouter.js
View file @
dc8b2cb
...
...
@@ -11,6 +11,8 @@ import {
githubLogin
,
postGithubLogin
,
getMe
,
facebookLogin
,
postFacebookLogin
,
}
from
"../controllers/userController"
;
import
{
onlyPublic
,
onlyPrivate
}
from
"../middlewares"
;
...
...
@@ -35,4 +37,14 @@ globalRouter.get(
postGithubLogin
);
globalRouter
.
get
(
routes
.
me
,
getMe
);
globalRouter
.
get
(
routes
.
facebook
,
facebookLogin
);
globalRouter
.
get
(
routes
.
facebookCallback
,
passport
.
authenticate
(
"facebook"
,
{
failureRedirect
:
"/login"
,
}),
postFacebookLogin
);
export
default
globalRouter
;
...
...
routes.js
View file @
dc8b2cb
...
...
@@ -23,6 +23,10 @@ const DELETE_VIDEO = "/:id/delete";
const
GITHUB
=
"/auth/github"
;
const
GITHUB_CALLBACK
=
"/auth/github/callback"
;
// Facebook
const
FB
=
"/auth/facebook"
;
const
FB_CALLBACK
=
"/auth/facebook/callback"
;
const
routes
=
{
home
:
HOME
,
join
:
JOIN
,
...
...
@@ -65,6 +69,8 @@ const routes = {
gitHub
:
GITHUB
,
githubCallback
:
GITHUB_CALLBACK
,
me
:
ME
,
facebook
:
FB
,
facebookCallback
:
FB_CALLBACK
,
};
// template에서 직접 접근이 필요한 경우 함수로 바꿔준다.
export
default
routes
;
...
...
views/partials/socialLogin.pug
View file @
dc8b2cb
...
...
@@ -5,6 +5,7 @@
i.fab.fa-github
| Continue with Github
button.social-login--facebook
span
i.fab.fa-facebook
| Continue with Facebook
\ No newline at end of file
a(href=routes.facebook)
span
i.fab.fa-facebook
| Continue with Facebook
\ No newline at end of file
...
...
Please
register
or
login
to post a comment