42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
var express = require('express');
|
|
var router = express.Router();
|
|
const passport = require('passport');
|
|
const accounts = require('./../database/accounts/accounts');
|
|
const app = require('../app');
|
|
|
|
|
|
function adminLoggedIn(req, res, next) {
|
|
if (req.user && req.user[2]) {
|
|
next();
|
|
}
|
|
else {
|
|
req.flash('error', 'An admin account is required to access this page.');
|
|
res.redirect('/auth/login');
|
|
}
|
|
}
|
|
|
|
router.get('/login', (req, res, next) => {
|
|
res.render('accounts/login', { title : "Login", message: req.flash('error') });
|
|
});
|
|
|
|
router.get('/logout', (req, res, next) => {
|
|
req.logout();
|
|
res.redirect("/");
|
|
});
|
|
|
|
router.post('/login',
|
|
passport.authenticate('local', {
|
|
failureRedirect: '/auth/login',
|
|
successRedirect: '/',
|
|
failureFlash: "Invalid email or password.",
|
|
}),
|
|
(req, res, next) => {
|
|
console.log(req.user);
|
|
});
|
|
|
|
router.post('/register', adminLoggedIn, (req, res, next) => {
|
|
accounts.createUser(req.body.email, req.body.password, !!req.body.admin)
|
|
.then(res.redirect('/manage'));
|
|
});
|
|
|
|
module.exports = router; |