2017-03-15 02:01:55 -06:00
'use strict' ;
2017-04-09 21:40:08 -06:00
const
mw = require ( './middleware.js' ) ,
2017-04-01 11:03:05 -06:00
mail = require ( './mail.js' ) ,
User = require ( './models.js' ) . user ,
env = require ( './env.js' ) ;
2016-03-31 17:06:21 -06:00
2017-04-01 11:03:05 -06:00
module . exports = function ( app , passport ) {
2017-04-09 21:40:08 -06:00
2017-04-01 11:03:05 -06:00
// Methods for success and failure
2017-04-09 21:40:08 -06:00
const
loginOutcome = {
2017-04-01 11:03:05 -06:00
failureRedirect : '/login' ,
failureFlash : true
2017-04-09 21:40:08 -06:00
} ,
connectOutcome = {
failureRedirect : '/settings' ,
2017-04-01 11:03:05 -06:00
failureFlash : true
2017-04-09 21:40:08 -06:00
} ,
loginCallback = function ( req , res ) {
2017-04-12 11:41:27 -06:00
res . redirect ( req . session . next || '/settings' ) ;
delete req . session . next ;
2017-04-09 21:40:08 -06:00
} ;
2017-04-01 11:03:05 -06:00
// Login/-out
app . route ( '/login' )
. get ( function ( req , res ) {
if ( req . isAuthenticated ( ) ) {
2017-04-12 11:41:27 -06:00
res . redirect ( '/settings' ) ; }
2017-04-01 11:03:05 -06:00
else { res . render ( 'login' ) ; }
} )
. post ( passport . authenticate ( 'local' , loginOutcome ) , loginCallback ) ;
app . get ( '/logout' , function ( req , res ) {
req . logout ( ) ;
res . redirect ( '/' ) ;
} ) ;
2016-07-01 19:14:36 -06:00
2017-04-01 11:03:05 -06:00
// Signup
app . post ( '/signup' , function ( req , res , next ) {
User . findOne ( { 'email' : req . body . email } , function ( err , user ) {
if ( err ) { next ( err ) ; }
2016-08-09 23:51:49 -06:00
2017-04-01 11:03:05 -06:00
// User already exists
else if ( user ) {
req . flash ( 'warning' , 'A user with that email already exists! If you forgot your password, use <a href="/login/forgot">this form</a>.' ) ;
res . redirect ( '/login' ) ;
} else {
// Create user
var newUser = new User ( ) ;
newUser . email = req . body . email ;
newUser . created = Date . now ( ) ;
newUser . createToken ( function ( err , token ) {
if ( err ) { next ( err ) ; }
mail ( {
2017-04-10 01:00:56 -06:00
from : '"Tracman" <NoReply@tracman.org>' ,
2017-04-01 11:03:05 -06:00
to : req . body . email ,
2017-04-10 01:00:56 -06:00
subject : 'Complete your Tracman registration' ,
text : ` Welcome to Tracman! \n \n To complete your registration, follow this link and set your password: \n ${ env . url } /settings/password/ ${ token } ` ,
html : ` <p>Welcome to Tracman! </p><p>To complete your registration, follow this link and set your password:<br><a href=" ${ env . url } /settings/password/ ${ token } "> ${ env . url } /settings/password/ ${ token } </a></p> `
2017-04-01 11:03:05 -06:00
} ) . then ( function ( ) {
req . flash ( 'success' , ` An email has been sent to <u> ${ req . body . email } </u>. Check your inbox to complete your registration. ` ) ;
res . redirect ( '/' ) ;
} ) . catch ( function ( err ) {
next ( err ) ;
} ) ;
2016-03-31 17:06:21 -06:00
} ) ;
2017-04-01 11:03:05 -06:00
}
} ) ;
} ) ;
// Forgot password
app . route ( '/login/forgot' )
. all ( function ( req , res , next ) {
2017-04-12 11:41:27 -06:00
if ( req . isAuthenticated ( ) ) { res . redirect ( '/settings' ) ; }
2017-04-01 11:03:05 -06:00
else { next ( ) ; }
} )
. get ( function ( req , res , next ) {
res . render ( 'forgot' ) ;
} )
. post ( function ( req , res , next ) {
2016-08-09 23:51:49 -06:00
2017-04-01 11:03:05 -06:00
//TODO: Validate and sanitize email
// req.assert('email', 'Please enter a valid email address.').isEmail();
// req.sanitize('email').normalizeEmail({ remove_dots: false });
User . findOne ( { 'email' : req . body . email } , function ( err , user ) {
if ( err ) { next ( err ) ; }
else if ( ! user ) {
req . flash ( 'danger' , ` No user has <u> ${ req . body . email } </u> set as their email address. ` ) ;
res . redirect ( '/login/forgot' ) ;
} else {
// Set reset token to user
user . createToken ( function ( err , token ) {
if ( err ) { next ( err ) ; }
// Email reset link
mail ( {
2017-04-10 01:00:56 -06:00
from : '"Tracman" <NoReply@tracman.org>' ,
to : ` " ${ user . name } "" < ${ user . email } > ` ,
subject : 'Reset your Tracman password' ,
2017-04-12 11:41:27 -06:00
text : ` Hi, \n \n Did you request to reset your Tracman password? If so, follow this link to do so: \n ${ env . url } /settings/password/ ${ token } \n \n If you didn't initiate this request, just ignore this email. ` ,
html : ` <p>Hi, </p><p>Did you request to reset your Tracman password? If so, follow this link to do so:<br><a href=" ${ env . url } /settings/password/ ${ token } "> ${ env . url } /settings/password/ ${ token } </a></p><p>If you didn't initiate this request, just ignore this email. </p> `
2017-04-01 11:03:05 -06:00
} ) . then ( function ( ) {
req . flash ( 'success' , ` An email has been sent to <u> ${ req . body . email } </u>. Check your email for instructions to reset your password. ` ) ;
res . redirect ( '/' ) ;
} ) . catch ( function ( err ) {
next ( err ) ;
} ) ;
2016-03-31 17:06:21 -06:00
} ) ;
2016-06-30 14:40:21 -06:00
}
2016-07-01 19:14:36 -06:00
} ) ;
2016-03-31 15:01:27 -06:00
2017-04-01 11:03:05 -06:00
} ) ;
2017-04-09 21:40:08 -06:00
2017-04-01 11:03:05 -06:00
// Social
app . get ( '/login/:service' , function ( req , res , next ) {
var service = req . params . service ;
if ( service === 'google' ) {
var sendParams = { scope : [ 'profile' ] } ;
}
if ( ! req . user ) { // Social login
passport . authenticate ( service , sendParams ) ( req , res , next ) ;
} else if ( ! req . user . auth [ service ] ) { // Connect social account
passport . authorize ( service , sendParams ) ( req , res , next ) ;
} else { // Disconnect social account
req . user . auth [ service ] = undefined ;
req . user . save ( function ( err ) {
2017-04-12 11:41:27 -06:00
if ( err ) { mw . throwErr ( err , req ) ; }
2017-04-01 11:03:05 -06:00
else {
req . flash ( 'success' , ` ${ mw . capitalize ( service ) } account disconnected. ` ) ;
}
2017-04-12 11:41:27 -06:00
res . redirect ( '/settings' ) ;
2016-03-31 17:06:21 -06:00
} ) ;
2017-04-01 11:03:05 -06:00
2016-03-31 17:06:21 -06:00
}
} ) ;
2017-04-01 11:03:05 -06:00
app . get ( '/login/:service/cb' , function ( req , res , next ) {
var service = req . params . service ;
if ( ! req . user ) {
passport . authenticate ( service , loginOutcome ) ( req , res , next ) ;
} else {
req . flash ( 'success' , ` ${ mw . capitalize ( service ) } account connected. ` ) ;
2017-04-12 11:41:27 -06:00
req . session . next = '/settings' ;
2017-04-01 11:03:05 -06:00
passport . authenticate ( service , connectOutcome ) ( req , res , next ) ;
}
} , loginCallback ) ;
// Old google auth
// app.get('/auth/google', passport.authenticate('google', { scope: [
// 'https://www.googleapis.com/auth/plus.login',
// 'https://www.googleapis.com/auth/plus.profile.emails.read'
// ] }));
// app.get('/auth/google/callback', passport.authenticate('google', {
// failureRedirect: '/',
// failureFlash: true,
// successRedirect: '/',
// successFlash: true
// } ));
// Android auth
//TODO: See if there's a better method
app . get ( '/auth/google/idtoken' , passport . authenticate ( 'google-id-token' ) , function ( req , res ) {
if ( ! req . user ) { res . sendStatus ( 401 ) ; }
else { res . send ( req . user ) ; }
} ) ;
} ;
// passport.use(new GoogleStrategy({
// clientID: env.googleClientId,
// clientSecret: env.googleClientSecret,
// callbackURL: env.url+'/auth/google/callback',
// passReqToCallback: true
// }, function(req, accessToken, refreshToken, profile, done) {
// // Check for user
// User.findOne({googleID: profile.id}, function(err, user){
// // Error
// if (err) { console.log('Error finding user with google ID: '+profile.id+'\n'+err); }
// // User found
// if (!err && user !== null) /* Log user in */ {
// if (!user.name) { user.name=profile.displayName; }
// user.lastLogin = Date.now();
// user.save(function (err, raw) {
2017-04-12 11:41:27 -06:00
// if (err) { throwErr(err,req); }
2017-04-01 11:03:05 -06:00
// }); done(null, user);
// }
// // User not found
// else /* create user */ {
// user = new User();
// user.googleID = profile.id;
// user.name = profile.displayName;
// user.email = profile.emails[0].value;
// user.slug = slug(profile.displayName).toLowerCase();
// user.created = Date.now();
// user.lastLogin = Date.now();
// // user.settings = { units:'standard', defaultMap:'road', defaultZoom:11, showSpeed:false, showTemp:false, showAlt:false, showStreetview:false },
// // user.last = { lat:0, lon:0, dir:0, alt:0, spd:0 },
// // user.isPro = false;
// // user.isAdmin = false;
// var cbc = 2;
// var successMessage, failMessage;
// // Generate slug
// (function checkSlug(s,cb) {
// //console.log('checking ',s);
// User.findOne({slug:s}, function(err, existingUser){
// if (err) { console.log('No user found for ',slug,':',err); }
// if (existingUser){
// s = '';
// while (s.length<6) {
// s+='abcdefghijkmnpqrtuvwxy346789'.charAt(Math.floor(Math.random()*28));
// }
// checkSlug(s,cb);
// } else { cb(s); }
// });
// })(user.slug, function(newSlug){
// user.slug = newSlug;
// if (cbc>1) /* waiting on other calls */ { cbc--; }
// else { done(null, user, { success:successMessage, failure:failMessage }); }
// });
// // Generate sk32
// crypto.randomBytes(32, function(err,buf) {
// if (err) {console.log('Unable to get random bytes:',err);}
// if (!buf) {console.log('Unable to get random buffer');}
// else {
// user.sk32 = buf.toString('hex');
// user.save(function(err) {
// if (err) {
// console.log('Error saving new user '+err);
// var failMessage = 'Something went wrong creating your account. Would you like to <a href="/bug">report this error</a>?';
// } else { successMessage = 'Your account has been created. Next maybe you should download the <a href="/android">android app</a>. ' }
// if (cbc>1) /* waiting on other calls */ { cbc--; }
// else { done(null, user, { success:successMessage, failure:failMessage }); }
// });
// }
// });
// }
// });
// }));
// passport.use(new GoogleTokenStrategy({
// clientID: env.googleClientId
// }, function(parsedToken, googleId, done) {
// User.findOne({googleID:googleId}, function(err, user) {
// if (err) {
// console.log('Error finding user for gToken login with google profile ID: '+googleId+'\n'+err); }
// if (!err && user !== null) { // Log in
// user.lastLogin = Date.now();
// user.save(function (err) {
// if (err) {
// console.log('Error saving user\'s lastLogin for gToken login with google profile ID: '+googleId+'\n'+err); }
// });
// return done(err, user);
// } else { // No such user
// done(null, false);
// }
// });
// }));