Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var passport = require('passport');
- var Strategy = require('passport-local').Strategy;
- var db = require('./db');
- const https = require('https');
- const fs = require('fs');
- const Db = require('./db/db.js');
- var DB = new Db();
- const privateKey = fs.readFileSync('./certificates/selfsigned.key', 'utf8');
- const certificate = fs.readFileSync('./certificates/selfsigned.crt', 'utf8');
- // Configure the local strategy for use by Passport.
- //
- // The local strategy require a `verify` function which receives the credentials
- // (`username` and `password`) submitted by the user. The function must verify
- // that the password is correct and then invoke `cb` with a user object, which
- // will be set at `req.user` in route handlers after authentication.
- passport.use(new Strategy(
- function(username, password, cb) {
- db.users.findByUsername(username,password, function(err, signedin) {
- if (err) { return cb(err); }
- if (!signedin) { return cb(null, false); }
- return cb(null, signedin);
- });
- }));
- // Configure Passport authenticated session persistence.
- //
- // In order to restore authentication state across HTTP requests, Passport needs
- // to serialize users into and deserialize users out of the session. The
- // typical implementation of this is as simple as supplying the user ID when
- // serializing, and querying the user record by ID from the database when
- // deserializing.
- passport.serializeUser(function(user, cb) {
- cb(null, user.id);
- });
- passport.deserializeUser(function(id, cb) {
- db.users.findById(id, function (err, user) {
- if (err) { return cb(err); }
- cb(null, user);
- });
- });
- // Create a new Express application.
- var app = express();
- // Configure view engine to render EJS templates.
- app.set('views', __dirname + '/public/views');
- app.set('view engine', 'ejs');
- // Use application-level middleware for common functionality, including
- // logging, parsing, and session handling.
- app.use(express.static(__dirname + '/public'));
- app.use(require('morgan')('combined'));
- app.use(require('cookie-parser')());
- app.use(require('body-parser').urlencoded({ extended: true }));
- app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
- // Initialize Passport and restore authentication state, if any, from the
- // session.
- app.use(passport.initialize());
- app.use(passport.session());
- // Define routes.
- app.get('/',
- function(req, res) {
- DB.sql_query("SELECT * FROM `Bus`",function (result){
- console.log("test: ", result[0].idBus);
- DB.sql_query("SELECT * FROM 'Users'", function(resultUsers) {
- res.render('home', { user: req.user, data: result, userData: resultUsers });
- })
- });
- app.get('/login',
- function(req, res){
- res.render('login');
- });
- app.post('/login',
- passport.authenticate('local', { failureRedirect: '/login' }),
- function(req, res) {
- res.redirect('/');
- });
- app.get('/logout',
- function(req, res){
- req.logout();
- res.redirect('/login');
- });
- var options = {key: privateKey, cert: certificate};
- var server = https.createServer(options, app);
- server.listen(8443, function listening () {
- console.log(""+server.address().port);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement