2018-05-17 20:22:09 +00:00
|
|
|
var passport = require('passport');
|
|
|
|
var FacebookStrategy = require('passport-facebook').Strategy;
|
2018-05-30 21:51:57 +00:00
|
|
|
var User = require('../models/user');
|
2018-06-08 02:57:29 +00:00
|
|
|
var jwt = require('jsonwebtoken');
|
|
|
|
var config = require('../config/database');
|
|
|
|
|
2018-06-07 06:44:15 +00:00
|
|
|
var mongoose = require('mongoose');
|
2018-06-08 02:57:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
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}`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-17 20:22:09 +00:00
|
|
|
passport.use(new FacebookStrategy({
|
|
|
|
clientID: "191092458179642",
|
|
|
|
clientSecret: "964e490d7e49a13ee7a77c9b65419fb3",
|
2018-06-08 02:57:29 +00:00
|
|
|
callbackURL: "https://localhost:3000/api/facebook/callback",
|
|
|
|
proxy: true
|
2018-05-17 20:22:09 +00:00
|
|
|
},
|
|
|
|
function(accessToken, refreshToken, profile, done) {
|
2018-06-07 06:44:15 +00:00
|
|
|
console.log('facebook findOneOrCreate'+profile.displayName);
|
2018-06-08 02:57:29 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2018-06-07 06:44:15 +00:00
|
|
|
}
|
2018-06-08 02:57:29 +00:00
|
|
|
|
2018-06-07 06:44:15 +00:00
|
|
|
});
|
2018-06-08 02:57:29 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
// });
|
2018-06-07 06:44:15 +00:00
|
|
|
// 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
|
|
|
|
// })
|
2018-05-17 20:22:09 +00:00
|
|
|
}
|
2018-06-07 06:44:15 +00:00
|
|
|
// 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);
|
|
|
|
// });*/
|
|
|
|
// }
|
2018-05-17 20:22:09 +00:00
|
|
|
));
|
2018-05-30 22:37:35 +00:00
|
|
|
module.exports = passport;
|