2018-12-08 19:21:48 +01:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const Models = require('../../models');
|
|
|
|
const bCrypt = require('bcrypt-nodejs');
|
2018-10-31 17:00:31 +01:00
|
|
|
//--------------------------
|
|
|
|
// Index
|
|
|
|
//--------------------------
|
2018-11-11 21:22:01 +01:00
|
|
|
router.get('/', function(req, res)
|
|
|
|
{
|
2018-10-31 17:00:31 +01:00
|
|
|
var loggedIn = false
|
2018-11-11 21:22:01 +01:00
|
|
|
if (req.session.user) loggedIn = true;
|
2018-12-11 18:35:29 +01:00
|
|
|
Models.FreshPost.findAll(
|
2018-11-11 21:22:01 +01:00
|
|
|
{
|
2018-12-11 18:35:29 +01:00
|
|
|
order: [
|
|
|
|
['id', 'DESC']
|
2019-01-31 18:36:59 +01:00
|
|
|
]
|
2018-12-11 18:35:29 +01:00
|
|
|
}).then(function(posts)
|
|
|
|
{
|
2019-02-21 17:35:44 +01:00
|
|
|
let title = 'Fipamo Admin';
|
|
|
|
(!loggedIn) ? welcome = "Hello." : welcome = "Welcome back, "+req.session.user.handle
|
2018-12-11 18:35:29 +01:00
|
|
|
let filtered = [];
|
|
|
|
for (let index = 0; index < posts.length; index++)
|
2018-11-11 21:22:01 +01:00
|
|
|
{
|
2018-12-11 18:35:29 +01:00
|
|
|
let item = posts[index].post;
|
|
|
|
if (typeof item.deleted == 'undefined' || item.deleted == false)
|
2018-12-04 03:19:10 +01:00
|
|
|
{
|
2018-12-11 18:35:29 +01:00
|
|
|
filtered.push(posts[index])
|
2018-12-04 03:19:10 +01:00
|
|
|
}
|
2018-12-11 18:35:29 +01:00
|
|
|
else
|
2018-11-11 21:22:01 +01:00
|
|
|
{
|
2018-12-11 18:35:29 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.render('dash/index',
|
2018-11-11 21:22:01 +01:00
|
|
|
{
|
2019-01-31 18:36:59 +01:00
|
|
|
title: title,
|
2019-02-21 17:35:44 +01:00
|
|
|
welcome: welcome,
|
2018-12-11 18:35:29 +01:00
|
|
|
user_status: loggedIn,
|
|
|
|
items: filtered
|
2018-10-31 17:00:31 +01:00
|
|
|
});
|
2018-12-11 18:35:29 +01:00
|
|
|
}).then(function(value)
|
2018-11-11 21:22:01 +01:00
|
|
|
{
|
2018-12-11 18:35:29 +01:00
|
|
|
//console.log(value);
|
|
|
|
}).catch(function(err)
|
|
|
|
{
|
|
|
|
//next(err);
|
|
|
|
})
|
2018-10-31 17:00:31 +01:00
|
|
|
});
|
|
|
|
//--------------------------
|
2018-12-08 19:21:48 +01:00
|
|
|
// Login
|
2018-10-31 17:00:31 +01:00
|
|
|
//--------------------------
|
2018-12-08 19:21:48 +01:00
|
|
|
/* 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'
|
2018-10-31 17:00:31 +01:00
|
|
|
});
|
2018-12-08 19:21:48 +01:00
|
|
|
}
|
|
|
|
let session = req.session;
|
|
|
|
session.user = user;
|
|
|
|
res.redirect('/@/dashboard');
|
|
|
|
}).catch(err =>
|
|
|
|
{
|
|
|
|
return res.json(
|
|
|
|
{
|
|
|
|
message: 'NOT FOUND, HAWS'
|
2018-10-31 17:00:31 +01:00
|
|
|
});
|
2018-12-08 19:21:48 +01:00
|
|
|
})
|
2018-10-31 17:00:31 +01:00
|
|
|
});
|
|
|
|
//--------------------------
|
2018-12-08 19:21:48 +01:00
|
|
|
// Logout
|
2018-10-31 17:00:31 +01:00
|
|
|
//--------------------------
|
2018-12-08 19:21:48 +01:00
|
|
|
router.post('/logout', function(req, res, next)
|
|
|
|
{
|
|
|
|
req.logout();
|
|
|
|
return res.json(
|
|
|
|
{
|
|
|
|
message: 'LOGGED OUT'
|
|
|
|
});
|
2018-10-31 17:00:31 +01:00
|
|
|
});
|
2018-12-08 19:21:48 +01:00
|
|
|
module.exports = router;
|
|
|
|
var isValidPassword = function(user, password)
|
|
|
|
{
|
|
|
|
return bCrypt.compareSync(password, user.password);
|
|
|
|
}
|