I was building user authentication following this tutorial. In here the writer has the passport.js file in the config directory, along with the authentication tokens, and database login string. I understand why the database and auth config's should not be public, so normally I would .gitignore the entire config directory. However this passport.js file is code that I would like to have version control on. Is there any reason this file doesn't belong in public version control, and if so how should I go about removing just that content so the logic of the code can be checked in.
Passport.JS
var LocalStrategy = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var User = require('../app/models/user');
var configAuth = require('../config/auth');
module.exports = function(passport){
passport.serializeUser(function(user, done){
done(null, user.id);
});
passport.deserializeUser(function(id, done){
User.findById(id,function(err, user){
done(err, user);
});
});
passport.use(new GoogleStrategy(
{
clientID: configAuth.googleAuth.clientID,
clientSecret: configAuth.googleAuth.clientSecret,
callbackURL: configAuth.googleAuth.callbackURL,
passReqToCallback: true
},
function(req, token, refreshToken, profile, done){
process.nextTick(function(){
if(!req.user)
{
User.findOne({'google.id': profile.id}, function(err, user){
if(err)
{
return done(err);
}
if(user)
{
return done(null, user);
}
else
{
var newUser = new User();
newUser.account.google_id = profile.id;
newUser.account.google_token = token;
newUser.account.name = profile.displayName;
newUser.account.email = profile.emails[0].value;
newUser.save(function(err){
if(err)
{
throw err;
}
return done(null, newUser);
});
}
});
}
else
{
var user = req.user;
user.account.google_id = profile.id;
user.account.google_token = token;
user.account.name = profile.displayName;
user.account.email = profile.emails[0].value;
user.save(function(err){
if(err)
{
throw err;
}
return done(null, user);
});
}
});
}
));
passport.use(new TwitterStrategy(
{
consumerKey: configAuth.twitterAuth.consumerKey,
consumerSecret: configAuth.twitterAuth.consumerSecret,
callbackURL: configAuth.callbackURL,
passReqToCallback: true
},
function(req, token, tokenSecret, profile, done){
console.log(token);
process.nextTick(function(){
if(!req.user)
{
User.findOne({'twitter.id': profile.id }, function(err, user){
if(err)
{
return done(err);
}
if(user)
{
return done(null, user);
}
else
{
var newUser = new User();
newUser.account.twitter_id = profile.id;
newUser.account.twitter_token = token;
newUser.account.twitter_username = profile.username;
console.log(newUser);
newUser.save(function(err){
if(err)
{
throw err;
}
return done(null, newUser);
});
}
});
}
else{
var user = req.user;
user.account.twitter_id = profile.id;
user.account.twitter_token = token;
user.account.twitter_username = profile.username;
user.save(function(err){
if(err)
{
throw err;
}
return done(null, user);
});
}
});
}
));
passport.use(new FacebookStrategy(
{
clientID: configAuth.facebookAuth.clientID,
clientSecret: configAuth.facebookAuth.clientSecret,
callbackURL : configAuth.facebookAuth.callbackURL,
passReqToCallback: true,
profileFields: ["email", "name"],
},
function(req, token, refreshToken, profile, done){
process.nextTick(function(){
if(!req.user)
{
User.findOne({'facebook.id' : profile.id}, function(err, user){
if(err)
{
return done(err);
}
if(user)
{
return done(null, user);
}
else
{
var newUser = new User();
newUser.account.facebook_id = profile.id;
newUser.account.facebook_token = token;
newUser.account.name = profile.displayName;
newUser.save(function(err){
if(err)
{
throw err;
}
return done(null, newUser);
});
}
});
}
else
{
var user = req.user;
user.account.facebook_id = profile.id;
user.account.facebook_token = token;
user.account.name = profile.displayName;
user.save(function(err){
if(err)
{
throw err;
}
return done(null, user);
});
}
});
}
));
passport.use('local-signup', new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done){
process.nextTick(function(){
User.findOne({'local.email': email}, function(err, user){
if(err)
{
return done(err);
}
if(user){
return done(null, false, req.flash('signupMessage',' That email is already taken'));
}
else
{
var newUser = new User();
newUser.account.email = email;
newUser.account.password = newUser.generateHash(password);
newUser.save(function(err){
if(err)
{
throw err;
}
return done(null, newUser);
});
}
});
});
}));
passport.use('local-login', new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
passReqToCallback : true
},
function(req, email, password, done){
User.findOne({'local.email': email}, function(err, user){
if(err)
{
return done(err);
}
if(!user)
{
return done(null, false, req.flash('loginMessage', 'Username not found!'));
}
if (!user.validPassword(password))
{
return done(null, false, req.flash('loginMessage', 'Incorrect Password'));
}
return done(null, user);
});
}
));
}
via Justin Olson
No comments:
Post a Comment