Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. var express = require('express');
  2. var passport = require('passport');
  3. var Strategy = require('passport-local').Strategy;
  4. var db = require('./db');
  5. const https = require('https');
  6. const fs = require('fs');
  7.  
  8. const Db = require('./db/db.js');
  9. var DB = new Db();
  10. const privateKey = fs.readFileSync('./certificates/selfsigned.key', 'utf8');
  11. const certificate = fs.readFileSync('./certificates/selfsigned.crt', 'utf8');
  12.  
  13.  
  14.  
  15.  
  16. // Configure the local strategy for use by Passport.
  17. //
  18. // The local strategy require a `verify` function which receives the credentials
  19. // (`username` and `password`) submitted by the user. The function must verify
  20. // that the password is correct and then invoke `cb` with a user object, which
  21. // will be set at `req.user` in route handlers after authentication.
  22. passport.use(new Strategy(
  23. function(username, password, cb) {
  24. db.users.findByUsername(username,password, function(err, signedin) {
  25. if (err) { return cb(err); }
  26. if (!signedin) { return cb(null, false); }
  27. return cb(null, signedin);
  28. });
  29. }));
  30.  
  31. // Configure Passport authenticated session persistence.
  32. //
  33. // In order to restore authentication state across HTTP requests, Passport needs
  34. // to serialize users into and deserialize users out of the session. The
  35. // typical implementation of this is as simple as supplying the user ID when
  36. // serializing, and querying the user record by ID from the database when
  37. // deserializing.
  38.  
  39. passport.serializeUser(function(user, cb) {
  40. cb(null, user.id);
  41. });
  42.  
  43. passport.deserializeUser(function(id, cb) {
  44. db.users.findById(id, function (err, user) {
  45. if (err) { return cb(err); }
  46. cb(null, user);
  47. });
  48. });
  49.  
  50.  
  51.  
  52. // Create a new Express application.
  53. var app = express();
  54.  
  55. // Configure view engine to render EJS templates.
  56. app.set('views', __dirname + '/public/views');
  57. app.set('view engine', 'ejs');
  58.  
  59. // Use application-level middleware for common functionality, including
  60. // logging, parsing, and session handling.
  61. app.use(express.static(__dirname + '/public'));
  62. app.use(require('morgan')('combined'));
  63. app.use(require('cookie-parser')());
  64. app.use(require('body-parser').urlencoded({ extended: true }));
  65. app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
  66.  
  67. // Initialize Passport and restore authentication state, if any, from the
  68. // session.
  69. app.use(passport.initialize());
  70. app.use(passport.session());
  71.  
  72. // Define routes.
  73. app.get('/',
  74. function(req, res) {
  75. DB.sql_query("SELECT * FROM `Bus`",function (result){
  76. console.log("test: ", result[0].idBus);
  77. DB.sql_query("SELECT * FROM 'Users'", function(resultUsers) {
  78. res.render('home', { user: req.user, data: result, userData: resultUsers });
  79. })
  80. });
  81.  
  82. app.get('/login',
  83. function(req, res){
  84. res.render('login');
  85. });
  86.  
  87. app.post('/login',
  88. passport.authenticate('local', { failureRedirect: '/login' }),
  89. function(req, res) {
  90. res.redirect('/');
  91. });
  92.  
  93. app.get('/logout',
  94. function(req, res){
  95. req.logout();
  96. res.redirect('/login');
  97. });
  98.  
  99.  
  100.  
  101. var options = {key: privateKey, cert: certificate};
  102. var server = https.createServer(options, app);
  103.  
  104. server.listen(8443, function listening () {
  105. console.log(""+server.address().port);
  106. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement