added new auth route, implemented token authentication, new static files folder for rendered pages
This commit is contained in:
parent
f1b3077f02
commit
8bd3d9a41e
8 changed files with 154 additions and 38 deletions
59
brain/api/v1/auth.js
Normal file
59
brain/api/v1/auth.js
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
const bCrypt = require('bcrypt-nodejs');
|
||||||
|
const jwt = require('jsonwebtoken');
|
||||||
|
const Models = require('../../models');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Auth Status
|
||||||
|
*/
|
||||||
|
router.get('/', function(req, res) {
|
||||||
|
var token = req.headers['x-access-token'];
|
||||||
|
if (!token) return res.status(401).send({ auth: false, message: 'No token provided.' });
|
||||||
|
|
||||||
|
jwt.verify(token, 'super-secret-string', function(err, decoded) {
|
||||||
|
if (err)
|
||||||
|
return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
|
||||||
|
|
||||||
|
res.status(200).send(decoded);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
/**
|
||||||
|
* Login Member and return token
|
||||||
|
*/
|
||||||
|
router.post('/login', function(req, res) {
|
||||||
|
Models.User.findOne({
|
||||||
|
where: {
|
||||||
|
handle: req.body.handle
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(user => {
|
||||||
|
if (!isValidPassword(user, req.body.password)) {
|
||||||
|
return res.json({
|
||||||
|
message: 'CHECK YOUR PASSWORD'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let token = jwt.sign({ id: user._id }, 'super-secret-string', {
|
||||||
|
expiresIn: 86400 // expires in 24 hours
|
||||||
|
});
|
||||||
|
|
||||||
|
let session = req.session;
|
||||||
|
session.user = user;
|
||||||
|
session.token = token;
|
||||||
|
|
||||||
|
res.json({ auth: 'Yes', token: session.token });
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
return res.json({
|
||||||
|
message: 'NOT FOUND, HAWS'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
//router.post('/logout', function(req, res) {});
|
||||||
|
module.exports = router;
|
||||||
|
|
||||||
|
function isValidPassword(user, password) {
|
||||||
|
return bCrypt.compareSync(password, user.password);
|
||||||
|
}
|
|
@ -23,8 +23,9 @@ app.use(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
app.use(express.static(path.join(__dirname, '../content')));
|
app.use(express.static(path.join(__dirname, '../public'), { extensions: ['html'] }));
|
||||||
app.use(express.static(path.join(__dirname, '../themes')));
|
//app.use(express.static(path.join(__dirname, '../content')));
|
||||||
|
//app.use(express.static(path.join(__dirname, '../themes')));
|
||||||
app.use(
|
app.use(
|
||||||
session({
|
session({
|
||||||
store: new MemoryStore({
|
store: new MemoryStore({
|
||||||
|
@ -49,11 +50,13 @@ var navDashboard = require('./routes/back/dash_nav');
|
||||||
var postLibrary = require('./api/content/posts');
|
var postLibrary = require('./api/content/posts');
|
||||||
var settings = require('./api/content/settings');
|
var settings = require('./api/content/settings');
|
||||||
var mailer = require('./api/mail/mailer');
|
var mailer = require('./api/mail/mailer');
|
||||||
|
var auth = require('./api/v1/auth');
|
||||||
// API PATHS
|
// API PATHS
|
||||||
|
|
||||||
app.use('/api/post', postLibrary);
|
app.use('/api/post', postLibrary);
|
||||||
app.use('/api/settings', settings);
|
app.use('/api/settings', settings);
|
||||||
app.use('/api/mail', mailer);
|
app.use('/api/mail', mailer);
|
||||||
|
app.use('/api/v1/auth', auth);
|
||||||
// PAGES
|
// PAGES
|
||||||
app.use('/', front);
|
app.use('/', front);
|
||||||
app.use('/@/dashboard', back);
|
app.use('/@/dashboard', back);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const Models = require('../../models');
|
const Models = require('../../models');
|
||||||
const bCrypt = require('bcrypt-nodejs');
|
|
||||||
//--------------------------
|
//--------------------------
|
||||||
// Index
|
// Index
|
||||||
//--------------------------
|
//--------------------------
|
||||||
|
@ -40,32 +39,7 @@ router.get('/', function(req, res) {
|
||||||
//next(err);
|
//next(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
//--------------------------
|
|
||||||
// Login
|
|
||||||
//--------------------------
|
|
||||||
/* Handle Login POST */
|
|
||||||
router.post('/login', function(req, res) {
|
|
||||||
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(() => {
|
|
||||||
return res.json({
|
|
||||||
message: 'NOT FOUND, HAWS'
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//--------------------------
|
//--------------------------
|
||||||
// Logout
|
// Logout
|
||||||
//--------------------------
|
//--------------------------
|
||||||
|
@ -76,6 +50,3 @@ router.post('/logout', function(req, res) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
var isValidPassword = function(user, password) {
|
|
||||||
return bCrypt.compareSync(password, user.password);
|
|
||||||
};
|
|
||||||
|
|
90
package-lock.json
generated
90
package-lock.json
generated
|
@ -2056,6 +2056,11 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"buffer-equal-constant-time": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
|
||||||
|
"integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
|
||||||
|
},
|
||||||
"buffer-from": {
|
"buffer-from": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz",
|
||||||
|
@ -2732,6 +2737,14 @@
|
||||||
"safer-buffer": "^2.1.0"
|
"safer-buffer": "^2.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"ecdsa-sig-formatter": {
|
||||||
|
"version": "1.0.11",
|
||||||
|
"resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
|
||||||
|
"integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
|
||||||
|
"requires": {
|
||||||
|
"safe-buffer": "^5.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"editorconfig": {
|
"editorconfig": {
|
||||||
"version": "0.15.3",
|
"version": "0.15.3",
|
||||||
"resolved": "https://registry.npmjs.org/editorconfig/-/editorconfig-0.15.3.tgz",
|
"resolved": "https://registry.npmjs.org/editorconfig/-/editorconfig-0.15.3.tgz",
|
||||||
|
@ -4598,6 +4611,30 @@
|
||||||
"graceful-fs": "^4.1.6"
|
"graceful-fs": "^4.1.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"jsonwebtoken": {
|
||||||
|
"version": "8.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz",
|
||||||
|
"integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==",
|
||||||
|
"requires": {
|
||||||
|
"jws": "^3.2.2",
|
||||||
|
"lodash.includes": "^4.3.0",
|
||||||
|
"lodash.isboolean": "^3.0.3",
|
||||||
|
"lodash.isinteger": "^4.0.4",
|
||||||
|
"lodash.isnumber": "^3.0.3",
|
||||||
|
"lodash.isplainobject": "^4.0.6",
|
||||||
|
"lodash.isstring": "^4.0.1",
|
||||||
|
"lodash.once": "^4.0.0",
|
||||||
|
"ms": "^2.1.1",
|
||||||
|
"semver": "^5.6.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"ms": {
|
||||||
|
"version": "2.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
||||||
|
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"jsprim": {
|
"jsprim": {
|
||||||
"version": "1.4.1",
|
"version": "1.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz",
|
||||||
|
@ -4618,6 +4655,25 @@
|
||||||
"promise": "^7.0.1"
|
"promise": "^7.0.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"jwa": {
|
||||||
|
"version": "1.4.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
|
||||||
|
"integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
|
||||||
|
"requires": {
|
||||||
|
"buffer-equal-constant-time": "1.0.1",
|
||||||
|
"ecdsa-sig-formatter": "1.0.11",
|
||||||
|
"safe-buffer": "^5.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"jws": {
|
||||||
|
"version": "3.2.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
|
||||||
|
"integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
|
||||||
|
"requires": {
|
||||||
|
"jwa": "^1.4.1",
|
||||||
|
"safe-buffer": "^5.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"kind-of": {
|
"kind-of": {
|
||||||
"version": "3.2.2",
|
"version": "3.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
|
||||||
|
@ -4672,6 +4728,26 @@
|
||||||
"resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.escaperegexp/-/lodash.escaperegexp-4.1.2.tgz",
|
||||||
"integrity": "sha1-ZHYsSGGAglGKw99Mz11YhtriA0c="
|
"integrity": "sha1-ZHYsSGGAglGKw99Mz11YhtriA0c="
|
||||||
},
|
},
|
||||||
|
"lodash.includes": {
|
||||||
|
"version": "4.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
|
||||||
|
"integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
|
||||||
|
},
|
||||||
|
"lodash.isboolean": {
|
||||||
|
"version": "3.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
|
||||||
|
"integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
|
||||||
|
},
|
||||||
|
"lodash.isinteger": {
|
||||||
|
"version": "4.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
|
||||||
|
"integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
|
||||||
|
},
|
||||||
|
"lodash.isnumber": {
|
||||||
|
"version": "3.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
|
||||||
|
"integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
|
||||||
|
},
|
||||||
"lodash.isplainobject": {
|
"lodash.isplainobject": {
|
||||||
"version": "4.0.6",
|
"version": "4.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
|
||||||
|
@ -4687,6 +4763,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz",
|
||||||
"integrity": "sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ=="
|
"integrity": "sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ=="
|
||||||
},
|
},
|
||||||
|
"lodash.once": {
|
||||||
|
"version": "4.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
|
||||||
|
"integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
|
||||||
|
},
|
||||||
"lodash.pickby": {
|
"lodash.pickby": {
|
||||||
"version": "4.6.0",
|
"version": "4.6.0",
|
||||||
"resolved": "https://registry.npmjs.org/lodash.pickby/-/lodash.pickby-4.6.0.tgz",
|
"resolved": "https://registry.npmjs.org/lodash.pickby/-/lodash.pickby-4.6.0.tgz",
|
||||||
|
@ -5543,7 +5624,7 @@
|
||||||
},
|
},
|
||||||
"proper-lockfile": {
|
"proper-lockfile": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/proper-lockfile/-/proper-lockfile-1.2.0.tgz",
|
"resolved": "http://registry.npmjs.org/proper-lockfile/-/proper-lockfile-1.2.0.tgz",
|
||||||
"integrity": "sha1-zv9d2J0+XxD7deHo52vHWAGlnDQ=",
|
"integrity": "sha1-zv9d2J0+XxD7deHo52vHWAGlnDQ=",
|
||||||
"requires": {
|
"requires": {
|
||||||
"err-code": "^1.0.0",
|
"err-code": "^1.0.0",
|
||||||
|
@ -5998,7 +6079,7 @@
|
||||||
},
|
},
|
||||||
"safe-regex": {
|
"safe-regex": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz",
|
"resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz",
|
||||||
"integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=",
|
"integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"optional": true,
|
"optional": true,
|
||||||
|
@ -6051,8 +6132,7 @@
|
||||||
"semver": {
|
"semver": {
|
||||||
"version": "5.7.1",
|
"version": "5.7.1",
|
||||||
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
|
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
|
||||||
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
|
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"send": {
|
"send": {
|
||||||
"version": "0.17.1",
|
"version": "0.17.1",
|
||||||
|
@ -6717,7 +6797,7 @@
|
||||||
},
|
},
|
||||||
"through": {
|
"through": {
|
||||||
"version": "2.3.8",
|
"version": "2.3.8",
|
||||||
"resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
"resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz",
|
||||||
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU="
|
"integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU="
|
||||||
},
|
},
|
||||||
"thunkify": {
|
"thunkify": {
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
"fs-extra": "latest",
|
"fs-extra": "latest",
|
||||||
"highlight.js": "^9.15.10",
|
"highlight.js": "^9.15.10",
|
||||||
"jsdom": "^12.2.0",
|
"jsdom": "^12.2.0",
|
||||||
|
"jsonwebtoken": "^8.5.1",
|
||||||
"lodash": "^4.17.15",
|
"lodash": "^4.17.15",
|
||||||
"mailgun-js": "^0.18.0",
|
"mailgun-js": "^0.18.0",
|
||||||
"markdown-it": "^8.4.1",
|
"markdown-it": "^8.4.1",
|
||||||
|
|
1
public/admin/index.html
Normal file
1
public/admin/index.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
This is the admin sections
|
1
public/index.html
Normal file
1
public/index.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
What up, doe
|
|
@ -13,6 +13,6 @@ html(xmlns='http://www.w3.org/1999/xhtml', lang='en', xml:lang="en")
|
||||||
.main-container#main-content
|
.main-container#main-content
|
||||||
block main-content
|
block main-content
|
||||||
script(src='/'+theme+'/assets/js/toolkit.min.js' type="text/javascript")
|
script(src='/'+theme+'/assets/js/toolkit.min.js' type="text/javascript")
|
||||||
script(src='/'+theme+'/assets/js/start.min.js' type="text/javascript")
|
script(src='/'+theme+'/assets/js/awujo.min.js' type="text/javascript")
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue