Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var path = require('path');
  3. var favicon = require('static-favicon');
  4. var logger = require('morgan');
  5. var methodOverride = require('method-override');
  6. var cookieParser = require('cookie-parser');
  7. var bodyParser = require('body-parser');
  8. var session = require('express-session');
  9. var bcrypt = require("bcryptjs");
  10. var flash      = require('connect-flash');
  11.  
  12. /* The following user credentials should NEVER be stored liked this!  Always put
  13.    your username/passwords in a database - not code!
  14.  
  15.    Of course, you'd probably also have MANY usernames and passwords!
  16. */
  17. var username = "guest";
  18. var password = "password";
  19. bcrypt.genSalt(10, function(err, salt) {
  20.     bcrypt.hash(password, salt, function(err, hash) {
  21.         password = hash;
  22.         console.log("Hashed password = " + password);
  23.     });
  24. });
  25.  
  26.  
  27. // Note, I hashed the password "password" and stored it here.  In a system that allowed users to register,
  28. // the user would type a plain text password ("password") and your code would hash it and store in a database.
  29.  
  30.  
  31.  
  32.  
  33.  
  34. var routes = require('./routes/index');
  35.  
  36. var app = express();
  37.  
  38. // view engine setup
  39. app.set('views', path.join(__dirname, 'views'));
  40. app.set('view engine', 'ejs');
  41.  
  42.  
  43.  
  44. app.use(favicon());
  45. app.use(logger('dev'));
  46. app.use(bodyParser.json());
  47. app.use(bodyParser.urlencoded());
  48. app.use(methodOverride());
  49. app.use(cookieParser());
  50. app.use(session({ secret: 'cmps369'}))
  51. app.use(express.static(path.join(__dirname, 'public')));
  52.  
  53. // Set up passport to help with user authentication (guest/password)
  54. var passport = require('passport');
  55. var LocalStrategy = require('passport-local').Strategy;
  56. app.use(passport.initialize());
  57. app.use(passport.session());
  58.  
  59.  
  60.  
  61.  
  62.  
  63. passport.use(new LocalStrategy(
  64.     {
  65.       usernameField: 'username',
  66.       passwordField: 'password'
  67.     },
  68.  
  69.     function(user, pswd, done) {
  70.         if ( user != username ) {
  71.             console.log("Username mismatch");
  72.             return done(null, false);
  73.         }
  74.  
  75.         bcrypt.compare(pswd, password, function(err, isMatch) {
  76.             if (err) return done(err);
  77.             if ( !isMatch ) {
  78.                 console.log("Password mismatch");
  79.             }
  80.             else {
  81.                 console.log("Valid credentials");
  82.             }
  83.             done(null, isMatch);
  84.         });
  85.       }
  86.   ));
  87.  
  88.   passport.serializeUser(function(username, done) {
  89.       // this is called when the user object associated with the session
  90.       // needs to be turned into a string.  Since we are only storing the user
  91.       // as a string - just return the username.
  92.       done(null, username);
  93.   });
  94.  
  95.   passport.deserializeUser(function(username, done) {
  96.       // normally we would find the user in the database and
  97.       // return an object representing the user (for example, an object
  98.       // that also includes first and last name, email, etc)
  99.       done(null, username);
  100.    });
  101.  
  102.  
  103. // Posts to login will have username/password form data.
  104. // passport will call the appropriate functions...
  105. routes.post('/login',
  106.     passport.authenticate('local', { successRedirect: '/start',
  107.                                      failureRedirect: '/login_fail',
  108.                                   })
  109. );
  110.  
  111. routes.get('/login', function (req, res) {
  112.   res.render('login', {});
  113. });
  114.  
  115. routes.get('/login_fail', function (req, res) {
  116.   res.render('login_fail', {});
  117. });
  118.  
  119. routes.get('/logout', function (req, res) {
  120.   req.logout();
  121.   res.redirect('/login');
  122. });
  123.  
  124.  
  125. app.use('/', routes);
  126.  
  127.  
  128. module.exports = app;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement