You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.1 KiB
JavaScript
94 lines
2.1 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const Models = require('../../models');
|
|
const bCrypt = require('bcrypt-nodejs');
|
|
//--------------------------
|
|
// Index
|
|
//--------------------------
|
|
router.get('/', function(req, res)
|
|
{
|
|
var loggedIn = false
|
|
if (req.session.user) loggedIn = true;
|
|
Models.FreshPost.findAll(
|
|
{
|
|
order: [
|
|
['id', 'DESC']
|
|
],
|
|
limit: 5
|
|
}).then(function(posts)
|
|
{
|
|
let filtered = [];
|
|
for (let index = 0; index < posts.length; index++)
|
|
{
|
|
let item = posts[index].post;
|
|
if (typeof item.deleted == 'undefined' || item.deleted == false)
|
|
{
|
|
filtered.push(posts[index])
|
|
}
|
|
else
|
|
{
|
|
continue
|
|
}
|
|
}
|
|
res.render('dash/index',
|
|
{
|
|
title: 'Dashboard',
|
|
user_status: loggedIn,
|
|
items: filtered
|
|
});
|
|
}).then(function(value)
|
|
{
|
|
//console.log(value);
|
|
}).catch(function(err)
|
|
{
|
|
//next(err);
|
|
})
|
|
});
|
|
//--------------------------
|
|
// Login
|
|
//--------------------------
|
|
/* Handle Login POST */
|
|
router.post('/login', function(req, res, next)
|
|
{
|
|
Models.User.findOne(
|
|
{
|
|
where:
|
|
{
|
|
handle: req.body.handle
|
|
}
|
|
}).then(user =>
|
|
{
|
|
if (!isValidPassword(user, req.body.password))
|
|
{
|
|
return res.json(
|
|
{
|
|
message: 'CHECK YOUR PASSWORD'
|
|
});
|
|
}
|
|
let session = req.session;
|
|
session.user = user;
|
|
res.redirect('/@/dashboard');
|
|
}).catch(err =>
|
|
{
|
|
return res.json(
|
|
{
|
|
message: 'NOT FOUND, HAWS'
|
|
});
|
|
})
|
|
});
|
|
//--------------------------
|
|
// Logout
|
|
//--------------------------
|
|
router.post('/logout', function(req, res, next)
|
|
{
|
|
req.logout();
|
|
return res.json(
|
|
{
|
|
message: 'LOGGED OUT'
|
|
});
|
|
});
|
|
module.exports = router;
|
|
var isValidPassword = function(user, password)
|
|
{
|
|
return bCrypt.compareSync(password, user.password);
|
|
} |