'use strict'; const mw = require('../middleware.js'), mail = require('../mail.js'), User = require('../models.js').user, crypto = require('crypto'), env = require('../env.js'); module.exports = (app, passport) => { // Methods for success and failure const loginOutcome = { failureRedirect: '/login', failureFlash: true }, connectOutcome = { failureRedirect: '/settings', failureFlash: true }, loginCallback = (req,res)=>{ res.redirect( req.session.next || '/map' ); }, androidLoginCallback = (req,res)=>{ if (req.user){ res.send(req.user); } else { res.sendStatus(401); } }; // Login/-out app.route('/login') .get( (req,res)=>{ // Already logged in if (req.isAuthenticated()) { loginCallback(req,res); } // Show login page else { res.render('login'); } }) .post( passport.authenticate('local',loginOutcome), loginCallback ); app.get('/logout', (req,res)=>{ req.logout(); req.flash('success',`You have been logged out.`); res.redirect( req.session.next || '/' ); }); // Signup app.route('/signup') .get( (req,res)=>{ res.redirect('/login#signup'); }) .post( (req,res,next)=>{ // Send token and alert user function sendToken(user){ // Create a password token user.createPassToken((err,token)=>{ if (err){ mw.throwErr(err,req); } // Email the instructions to continue mail.send({ from: mail.from, to: `<${user.email}>`, subject: 'Complete your Tracman registration', text: mail.text(`Welcome to Tracman! \n\nTo complete your registration, follow this link and set your password:\n${env.url}/settings/password/${token}`), html: mail.html(`
Welcome to Tracman!
To complete your registration, follow this link and set your password:
${env.url}/settings/password/${token}
Hi,
Did you request to reset your Tracman password? If so, follow this link to do so:
${env.url}/settings/password/${token}
If you didn't initiate this request, just ignore this email.
`) }).then(()=>{ req.flash('success', `If an account exists with the email ${req.body.email}, an email has been sent there with a password reset link. `); res.redirect('/login'); }).catch((err)=>{ mw.throwErr(err); }); }); } }).catch( (err)=>{ mw.throwErr(err,req); res.redirect('/login/forgot'); }); } ); // Social app.get('/login/:service', (req,res,next)=>{ let service = req.params.service, sendParams = (service==='google')? {scope:['profile']} : null; // Social login if (!req.user) { passport.authenticate(service, sendParams)(req,res,next); } // Connect social account else if (!req.user.auth[service]) { passport.authorize(service, sendParams)(req,res,next); } // Disconnect social account else { req.user.auth[service] = undefined; req.user.save() .catch((err)=>{ mw.throwErr(err,req); res.redirect('/settings'); }).then(()=>{ req.flash('success', `${mw.capitalize(service)} account disconnected. `); res.redirect('/settings'); }); } }); app.get('/login/:service/cb', (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. `); passport.authenticate(service, connectOutcome)(req,res,next); } }, loginCallback); // Android app.get('/login/android/', passport.authenticate('local'), androidLoginCallback); app.get('/login/android/google', passport.authenticate('google-id-token'), androidLoginCallback); //TODO: Add android facebook login //TODO: Add android twitter login };