{"size": 1516, "ext": "js", "max_stars_repo_path": "display/src/passport.js", "max_stars_repo_name": "ashryaagr/IBM-Hack-Challenge", "max_stars_repo_stars_event_min_datetime": "2020-03-12T18:48:33.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-29T17:34:50.000Z", "max_issues_repo_path": "display/src/passport.js", "max_issues_repo_name": "Gateway2745/IBM-Hack-Challenge", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "display/src/passport.js", "max_forks_repo_name": "Gateway2745/IBM-Hack-Challenge", "max_forks_repo_forks_event_min_datetime": "2019-07-23T14:21:50.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-06T09:04:22.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 8, "avg_line_length": 23.6875, "max_line_length": 74, "alphanum_fraction": 0.6668865435}
const passport = require('passport') ;
const User = require('./models/user') ;

passport.serializeUser(function(user, done) {
	done(null, user.id);
});

passport.deserializeUser(function(id, done) {
	User.findById(id, function(err, user) {
		done(err, user);
	});
});

var JwtStrategy = require('passport-jwt').Strategy,
	ExtractJwt = require('passport-jwt').ExtractJwt;
var opts = {} ;

var cookieExtractor = function(req) {
    var token = null;
    if (req && req.cookies)
    {
        token = req.cookies['jwt'];
    }
    return token;
};

opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken() ;
opts.secretOrKey = process.env.SECRET_KEY ;

passport.use('jwt', new JwtStrategy(opts, function(jwt_payload, done) {
	User.findOne({_id: jwt_payload._id}, function(err, user) {
		if (err) {
			return done(err, false);
		}
		if (user) {
			return done(null, user);
		} else {
			return done(null, false);
			// or you could create a new account
		}
	});
}));

opts = {} ;
opts.jwtFromRequest = ExtractJwt.fromExtractors([cookieExtractor]);
opts.secretOrKey = process.env.SECRET_KEY ;

passport.use('cookie', new JwtStrategy(opts, function(jwt_payload, done) {
	console.log("jwtpayload is : ", jwt_payload, jwt_payload.sub)
	User.findById(jwt_payload._id, function(err, user) {
		if (err) {
			return done(err, false);
		}
		console.log(user)
		if (user) {
			return done(null, user);
		} else {
			return done(null, false);
			// or you could create a new account
		}
	});
}));

module.exports = passport ;