Flare-k

[Authenticate] Facebook Login using passport

...@@ -80,13 +80,30 @@ export const postGithubLogin = (req, res) => { ...@@ -80,13 +80,30 @@ export const postGithubLogin = (req, res) => {
80 80
81 export const facebookLogin = passport.authenticate("facebook"); 81 export const facebookLogin = passport.authenticate("facebook");
82 82
83 -export const facebookLoginCallback = ( 83 +// http://www.passportjs.org/packages/passport-facebook/ 참고하였습니다.
84 - accessToken, 84 +export const facebookLoginCallback = async (_, __, profile, cb) => {
85 - refreshToken, 85 + const {
86 - profile, 86 + _json: { id, name, email },
87 - cb 87 + } = profile;
88 -) => { 88 + try {
89 - console.log(accessToken, refreshToken, profile, cb); 89 + const user = await User.findOne({ email });
90 + if (user) {
91 + user.facebookId = id;
92 + user.avatarUrl = `https://graph.facebook.com/${id}/picture?type=large`;
93 + user.save();
94 + return cb(null, user);
95 + }
96 + const newUser = await User.create({
97 + email,
98 + name,
99 + facebookId: id,
100 + avatarUrl: `https://graph.facebook.com/${id}/picture?type=large`,
101 + });
102 + return cb(null, newUser);
103 + } catch (error) {
104 + return cb(error, null);
105 + }
106 + // console.log(accessToken, refreshToken, profile, cb);
90 }; 107 };
91 108
92 export const postFacebookLogin = (req, res) => { 109 export const postFacebookLogin = (req, res) => {
......
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
6 "scripts": { 6 "scripts": {
7 "dev:server": "nodemon --exec babel-node init.js --delay 2 --ignore 'scss'", 7 "dev:server": "nodemon --exec babel-node init.js --delay 2 --ignore 'scss'",
8 "dev:assets": "WEBPACK_ENV=development webpack -w", 8 "dev:assets": "WEBPACK_ENV=development webpack -w",
9 - "build:assets": "WEBPACK_ENV=production webpack" 9 + "build:assets": "WEBPACK_ENV=production webpack",
10 + "tunnel": "ngrok http 80"
10 }, 11 },
11 "repository": { 12 "repository": {
12 "type": "git", 13 "type": "git",
...@@ -33,6 +34,7 @@ ...@@ -33,6 +34,7 @@
33 "mongoose": "^5.9.15", 34 "mongoose": "^5.9.15",
34 "morgan": "^1.10.0", 35 "morgan": "^1.10.0",
35 "multer": "^1.4.2", 36 "multer": "^1.4.2",
37 + "ngrok": "^3.2.7",
36 "node-sass": "^4.14.1", 38 "node-sass": "^4.14.1",
37 "passport": "^0.4.1", 39 "passport": "^0.4.1",
38 "passport-facebook": "^3.0.0", 40 "passport-facebook": "^3.0.0",
......
...@@ -30,7 +30,9 @@ passport.use( ...@@ -30,7 +30,9 @@ passport.use(
30 { 30 {
31 clientID: process.env.FB_ID, 31 clientID: process.env.FB_ID,
32 clientSecret: process.env.FB_SECRET, 32 clientSecret: process.env.FB_SECRET,
33 - callbackURL: `http://localhost:80${routes.facebookCallback}`, 33 + callbackURL: `https://77aa7153bcce.ngrok.io${routes.facebookCallback}`,
34 + profileFields: ["id", "displayName", "photos", "email"],
35 + scope: ["public_profile", "email"],
34 }, 36 },
35 facebookLoginCallback 37 facebookLoginCallback
36 ) 38 )
......