SHARE
TWEET

Untitled

a guest May 22nd, 2018 139 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --index.js
  2.  
  3. /* GET home page. */
  4. router.get('/', ensureAuthenticated,function(req, res, next) {
  5.   res.render('index', { title: 'Members' });
  6. });
  7.  
  8. function ensureAuthenticated(req,res,next){
  9.     if(req.isAuthenticated()){
  10.         var uname;
  11.         uname = req.user.name;
  12.         module.exports.uname = uname;
  13.         return next();
  14.     }
  15.     res.redirect('/users/login');
  16. }
  17.  
  18. router.post('/saveland',
  19.     ensureAuthenticated,
  20.   function(req, res) {
  21.     console.log('at saveland for ', req.user.name, 'saving', req.body.landname);
  22.  
  23.     User.getUserByUsername(req.user.name,function(err,user){
  24.         if(err){
  25.             console.log("some err", err);
  26.             throw err;
  27.  
  28.         }
  29.         if(!user){
  30.             console.log('no user found');
  31.             return;
  32.         }
  33.  
  34.         User.appendFav(user, req.body.landname, function(err){
  35.  
  36.             if(err) {
  37.                 console.log('some other err', err);
  38.                 return;
  39.             }
  40.             console.log('successfully added', req.body.landname, 'to ', req.user.name);
  41.  
  42.         });
  43.     });
  44.   });
  45.  
  46. module.exports = router;
  47.  
  48. --users.js
  49.  
  50. var express = require('express');
  51. var router = express.Router();
  52. var multer = require('multer');
  53. var upload = multer({dest: './uploads'});
  54. var passport = require('passport');
  55. var LocalStrategy = require('passport-local').Strategy;
  56. var User = require('../models/user');
  57.  
  58. /* GET users listing. */
  59. router.get('/', function(req, res, next) {
  60.   res.send('respond with a resource');
  61. });
  62.  
  63. router.get('/register', function(req, res, next) {
  64.   res.render('register',{title:'Register'});
  65. });
  66.  
  67. router.get('/login', function(req, res, next) {
  68.   res.render('login',{title:'Login'});
  69. });
  70.  
  71. router.post('/login',
  72.   passport.authenticate('local',{failureRedirect:'/users/login', failureFlash: 'Invalid username or password'}),
  73.   function(req, res) {
  74.  
  75.     req.flash('success','You are now logged in', req.body.username);
  76.     res.redirect('/');
  77.   });
  78.  
  79.  
  80. passport.serializeUser(function(user, done) {
  81.   done(null, user.id);
  82. });
  83.  
  84. passport.deserializeUser(function(id, done) {
  85.   User.getUserById(id, function(err, user) {
  86.     done(err, user);
  87.   });
  88. });
  89.  
  90.  
  91. passport.use(new LocalStrategy(function(username, password, done){
  92.     User.getUserByUsername(username,function(err,user){
  93.         if(err) throw err;
  94.         if(!user){
  95.             return done(null, false, {message: 'Unknown User' });
  96.         }
  97.  
  98.         User.comparePassword(password, user.password, function(err, isMatch){
  99.             if(err) return done(err);
  100.             if(isMatch){
  101.                 return done(null, user);
  102.             }else{
  103.                 return done(null, false, {message: 'invalid password'});
  104.             }
  105.         });
  106.     });
  107. }));
  108.  
  109.  
  110.  
  111. router.post('/register', upload.single('profileimage') , function(req, res, next) {
  112.     //console.log(req.body.name);
  113.     var name = req.body.name;
  114.     var email = req.body.email;
  115.     var username = req.body.username;
  116.     var password = req.body.password;
  117.     var password2 = req.body.password2;
  118.  
  119.     console.log(req.file);
  120.  
  121.     //form validator
  122.     req.checkBody('name','Name field is required').notEmpty();
  123.     req.checkBody('email','Email field is required').notEmpty();
  124.     req.checkBody('email','Email field is not valid').isEmail();
  125.     req.checkBody('username','Username field is required').notEmpty();
  126.     req.checkBody('password','Password field is required').notEmpty();
  127.     req.checkBody('password2','Passwords do not match').equals(req.body.password);
  128.  
  129.     //check errors
  130.     var errors = req.validationErrors();
  131.     console.log(errors);
  132.     if(errors){
  133.         //console.log('Errors');
  134.         res.render('register',{
  135.             errors: errors
  136.         });
  137.     }else{
  138.         console.log('no errors');
  139.         var newUser = new User({
  140.             name: name,
  141.             email: email,
  142.             username: username,
  143.             password: password,
  144.             favlands: []
  145.             //profileimage: profileimage
  146.         });
  147.  
  148.         User.createUser(newUser, function(err, user){
  149.             if(err) throw err;
  150.             console.log(user);
  151.         });
  152.  
  153.         req.flash('success', 'You are now registered and can login');
  154.  
  155.         res.location('/');
  156.         res.redirect('/');
  157.  
  158.     }
  159. });
  160.  
  161. router.get('/logout', function(req,res){
  162.     req.logout();
  163.     req.flash('success', 'You are now logged out');
  164.     res.redirect('/users/login');
  165. })
  166. //this allows us to accesss it from a different file
  167. module.exports = router;
  168.  
  169.  
  170. //bcrypt hashes the password
  171. //passport handles the login
  172. //Jade is an template engine, primarily used for server-side templating in NodeJS
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top