Flare-k

[Authenticate] Facebook Login

...@@ -78,6 +78,20 @@ export const postGithubLogin = (req, res) => { ...@@ -78,6 +78,20 @@ export const postGithubLogin = (req, res) => {
78 res.redirect(routes.home); 78 res.redirect(routes.home);
79 }; 79 };
80 80
81 +export const facebookLogin = passport.authenticate("facebook");
82 +
83 +export const facebookLoginCallback = (
84 + accessToken,
85 + refreshToken,
86 + profile,
87 + cb
88 +) => {
89 + console.log(accessToken, refreshToken, profile, cb);
90 +};
91 +
92 +export const postFacebookLogin = (req, res) => {
93 + res.redirect(routes.home);
94 +};
81 // 로그아웃을 클릭하면 LogOut페이지로 가는 것 대신에, 로그아웃을 처리한 후 95 // 로그아웃을 클릭하면 LogOut페이지로 가는 것 대신에, 로그아웃을 처리한 후
82 // home 페이지로 Redirect로 표현할 것이다. 96 // home 페이지로 Redirect로 표현할 것이다.
83 // 즉, 초반에 만들어둔 logout.pug는 삭제해도 좋다. 97 // 즉, 초반에 만들어둔 logout.pug는 삭제해도 좋다.
...@@ -91,8 +105,18 @@ export const getMe = (req, res) => ...@@ -91,8 +105,18 @@ export const getMe = (req, res) =>
91 res.render("userDetail", { pageTitle: "User Detail", user: req.user }); 105 res.render("userDetail", { pageTitle: "User Detail", user: req.user });
92 106
93 // export const users = (req, res) => res.render("users", { pageTitle: "Users" }); 107 // export const users = (req, res) => res.render("users", { pageTitle: "Users" });
94 -export const userDetail = (req, res) => 108 +
95 - res.render("userDetail", { pageTitle: "User Detail" }); 109 +export const userDetail = async (req, res) => {
110 + const {
111 + params: { id },
112 + } = req; // req로 부터 params의 id가져오기
113 + try {
114 + const user = await User.findById(id);
115 + res.render("userDetail", { pageTitle: "User Detail", user });
116 + } catch (error) {
117 + res.redirect(routes.home);
118 + }
119 +};
96 export const editProfile = (req, res) => 120 export const editProfile = (req, res) =>
97 res.render("editProfile", { pageTitle: "Edit Profile" }); 121 res.render("editProfile", { pageTitle: "Edit Profile" });
98 export const changePassword = (req, res) => 122 export const changePassword = (req, res) =>
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
35 "multer": "^1.4.2", 35 "multer": "^1.4.2",
36 "node-sass": "^4.14.1", 36 "node-sass": "^4.14.1",
37 "passport": "^0.4.1", 37 "passport": "^0.4.1",
38 + "passport-facebook": "^3.0.0",
38 "passport-github": "^1.1.0", 39 "passport-github": "^1.1.0",
39 "passport-local": "^1.0.0", 40 "passport-local": "^1.0.0",
40 "passport-local-mongoose": "^6.0.1", 41 "passport-local-mongoose": "^6.0.1",
......
1 import dotenv from "dotenv"; 1 import dotenv from "dotenv";
2 import passport from "passport"; 2 import passport from "passport";
3 import GithubStrategy from "passport-github"; 3 import GithubStrategy from "passport-github";
4 +import FacebookStrategy from "passport-facebook";
4 import User from "./models/User"; 5 import User from "./models/User";
5 -import { githubLoginCallback } from "./controllers/userController"; 6 +import {
7 + githubLoginCallback,
8 + facebookLoginCallback,
9 +} from "./controllers/userController";
6 import routes from "./routes"; 10 import routes from "./routes";
7 11
8 dotenv.config(); 12 dotenv.config();
...@@ -21,5 +25,16 @@ passport.use( ...@@ -21,5 +25,16 @@ passport.use(
21 ) 25 )
22 ); 26 );
23 27
28 +passport.use(
29 + new FacebookStrategy(
30 + {
31 + clientID: process.env.FB_ID,
32 + clientSecret: process.env.FB_SECRET,
33 + callbackURL: `http://localhost:80${routes.facebookCallback}`,
34 + },
35 + facebookLoginCallback
36 + )
37 +);
38 +
24 passport.serializeUser(User.serializeUser()); 39 passport.serializeUser(User.serializeUser());
25 passport.deserializeUser(User.deserializeUser()); 40 passport.deserializeUser(User.deserializeUser());
......
...@@ -11,6 +11,8 @@ import { ...@@ -11,6 +11,8 @@ import {
11 githubLogin, 11 githubLogin,
12 postGithubLogin, 12 postGithubLogin,
13 getMe, 13 getMe,
14 + facebookLogin,
15 + postFacebookLogin,
14 } from "../controllers/userController"; 16 } from "../controllers/userController";
15 import { onlyPublic, onlyPrivate } from "../middlewares"; 17 import { onlyPublic, onlyPrivate } from "../middlewares";
16 18
...@@ -35,4 +37,14 @@ globalRouter.get( ...@@ -35,4 +37,14 @@ globalRouter.get(
35 postGithubLogin 37 postGithubLogin
36 ); 38 );
37 globalRouter.get(routes.me, getMe); 39 globalRouter.get(routes.me, getMe);
40 +
41 +globalRouter.get(routes.facebook, facebookLogin);
42 +globalRouter.get(
43 + routes.facebookCallback,
44 + passport.authenticate("facebook", {
45 + failureRedirect: "/login",
46 + }),
47 + postFacebookLogin
48 +);
49 +
38 export default globalRouter; 50 export default globalRouter;
......
...@@ -23,6 +23,10 @@ const DELETE_VIDEO = "/:id/delete"; ...@@ -23,6 +23,10 @@ const DELETE_VIDEO = "/:id/delete";
23 const GITHUB = "/auth/github"; 23 const GITHUB = "/auth/github";
24 const GITHUB_CALLBACK = "/auth/github/callback"; 24 const GITHUB_CALLBACK = "/auth/github/callback";
25 25
26 +// Facebook
27 +const FB = "/auth/facebook";
28 +const FB_CALLBACK = "/auth/facebook/callback";
29 +
26 const routes = { 30 const routes = {
27 home: HOME, 31 home: HOME,
28 join: JOIN, 32 join: JOIN,
...@@ -65,6 +69,8 @@ const routes = { ...@@ -65,6 +69,8 @@ const routes = {
65 gitHub: GITHUB, 69 gitHub: GITHUB,
66 githubCallback: GITHUB_CALLBACK, 70 githubCallback: GITHUB_CALLBACK,
67 me: ME, 71 me: ME,
72 + facebook: FB,
73 + facebookCallback: FB_CALLBACK,
68 }; 74 };
69 // template에서 직접 접근이 필요한 경우 함수로 바꿔준다. 75 // template에서 직접 접근이 필요한 경우 함수로 바꿔준다.
70 export default routes; 76 export default routes;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
5 i.fab.fa-github 5 i.fab.fa-github
6 | Continue with Github 6 | Continue with Github
7 button.social-login--facebook 7 button.social-login--facebook
8 + a(href=routes.facebook)
8 span 9 span
9 i.fab.fa-facebook 10 i.fab.fa-facebook
10 | Continue with Facebook 11 | Continue with Facebook
...\ No newline at end of file ...\ No newline at end of file
......