var passport = require('passport'); var FacebookStrategy = require('passport-facebook').Strategy; var User = require('../models/user'); var jwt = require('jsonwebtoken'); var config = require('../config/database'); var mongoose = require('mongoose'); passport.serializeUser((user, done) => { done(null, user.id); }); passport.deserializeUser((id, done) => { // console.log(`id: ${id}`); User.findById(id) .then(user => { done(null, user); }) .catch(error => { console.log(`Error: ${error}`); }); }); passport.use(new FacebookStrategy({ clientID: "191092458179642", clientSecret: "964e490d7e49a13ee7a77c9b65419fb3", callbackURL: "https://localhost:3000/api/facebook/callback", proxy: true }, function(accessToken, refreshToken, profile, done) { console.log('facebook findOneOrCreate'+profile.displayName); User.findOne({ username: profile.displayName }, function(err, user) { if(user){ console.log('findOneOrCreate ok'+user.username); var token = jwt.sign(user.toJSON(), config.secret); // return the information including token as JSON //res.json({success: true, token: }); return done(null, user); }else{ var newUser = new User({ username: profile.displayName, password: profile.password, social: true }); // save the user newUser.save(function(err) { if (err) { console.log('Username already exists.'); } return done(null, user); }); } }); // User.findOneOrCreate(profile.displayName.toString(), function(err, user, req, res) { // if (err) { // console.log('findOneOrCreate err'); // console.log(err); // return done(err); // } // console.log('findOneOrCreate ok'+user.displayName); // //res.json({token: accessToken, user: user.username}); // done(null, user); // }); // Page.findOneOrCreate(pageId, (err, page)=>{ // if(err){ // //if theres an error, do something // } // // or do something with the page // }) // // // Page.findOneOrCreate(profile.id, (err, page)=>{ // if(err){ // //if theres an error, do something // } // // or do something with the page // }) } // function(accessToken, refreshToken, profile, done) { // console.log('yaaauuu : ' + accessToken + ' ----- ' + profile.displayName + ' ----- ' + profile.id); // //done(null, {profile, accessToken}); // // // var newUser = new User({ // username: req.body.username, // password: req.body.password // }); // // User.save(newUser, function(err, user) { // if (err) { // console.log('yaaauuu err : ' + err); // return done(err); // } // console.log('yaaauuu : '); // done(null, user); // }); // // // // /* // User.findOrCreate({username: profile.displayName}, {username: profile.displayName, id: profile.id}, function(err, user) { // console.log('yaaauuu err : ' + ' ----- ' + err ); // // console.log('yaaauuu user : ' + ' ----- ' + JSON.stringify(user)); // if (err) { return done(err); } // done(null, user); // });*/ // } )); module.exports = passport;