Flare-k

[Authenticate] Facebook Login using passport

......@@ -80,13 +80,30 @@ export const postGithubLogin = (req, res) => {
export const facebookLogin = passport.authenticate("facebook");
export const facebookLoginCallback = (
accessToken,
refreshToken,
profile,
cb
) => {
console.log(accessToken, refreshToken, profile, cb);
// http://www.passportjs.org/packages/passport-facebook/ 참고하였습니다.
export const facebookLoginCallback = async (_, __, profile, cb) => {
const {
_json: { id, name, email },
} = profile;
try {
const user = await User.findOne({ email });
if (user) {
user.facebookId = id;
user.avatarUrl = `https://graph.facebook.com/${id}/picture?type=large`;
user.save();
return cb(null, user);
}
const newUser = await User.create({
email,
name,
facebookId: id,
avatarUrl: `https://graph.facebook.com/${id}/picture?type=large`,
});
return cb(null, newUser);
} catch (error) {
return cb(error, null);
}
// console.log(accessToken, refreshToken, profile, cb);
};
export const postFacebookLogin = (req, res) => {
......
......@@ -6,7 +6,8 @@
"scripts": {
"dev:server": "nodemon --exec babel-node init.js --delay 2 --ignore 'scss'",
"dev:assets": "WEBPACK_ENV=development webpack -w",
"build:assets": "WEBPACK_ENV=production webpack"
"build:assets": "WEBPACK_ENV=production webpack",
"tunnel": "ngrok http 80"
},
"repository": {
"type": "git",
......@@ -33,6 +34,7 @@
"mongoose": "^5.9.15",
"morgan": "^1.10.0",
"multer": "^1.4.2",
"ngrok": "^3.2.7",
"node-sass": "^4.14.1",
"passport": "^0.4.1",
"passport-facebook": "^3.0.0",
......
......@@ -30,7 +30,9 @@ passport.use(
{
clientID: process.env.FB_ID,
clientSecret: process.env.FB_SECRET,
callbackURL: `http://localhost:80${routes.facebookCallback}`,
callbackURL: `https://77aa7153bcce.ngrok.io${routes.facebookCallback}`,
profileFields: ["id", "displayName", "photos", "email"],
scope: ["public_profile", "email"],
},
facebookLoginCallback
)
......