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;