var express = require('express');
var router = express.Router();
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const User = require('../models/user');
const bcrypt = require('bcrypt');
const saltRounds = 10;
passport.use('local',new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (!bcrypt.compare(user.password, password)) { return done(null, false); }
return done(null, user);
});
}
));
router.get("/", async function(req, res, next) {
res.render('index', {title: "Express"});
});
router.get("/main", async function(req, res, next) {
res.json({success: "success"});
});
router.get("/failure", async function(req, res, next) {
res.json({failure: "failure"});
});
router.post("/login", async function(req, res, next){
passport.authenticate('local', {
successRedirect: '/main',
failureRedirect: '/failure',
failureFlash: true
})(req, res, next);
});
module.exports = router;
/*....*/
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
const passport = require('passport');
var flash = require('connect-flash');
var session = require('express-session');
/*mongodb*/
// Set up mongoose connection
var mongoose = require('mongoose');
/*mongodb stuff*/
//////
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: 'unknown' })); // session secret
// Passport middleware
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
app.use('/', indexRouter);
app.use('/users', usersRouter);