4
1
Fork 0
This commit is contained in:
Alpha 2018-02-20 02:39:10 +01:00
commit 7cff726581
5 changed files with 82 additions and 8 deletions

View file

@ -1,15 +1,38 @@
var app = require('express')()
var http = require('http').Server(app)
var request = require('request')
var exphbs = require('express-handlebars')
var app = require('express')(),
session = require('express-session'),
http = require('http').Server(app),
request = require('request'),
exphbs = require('express-handlebars')
app.engine('.hbs', exphbs({defaultLayout: 'single', extname: '.hbs'}))
app.set('view engine', '.hbs')
// function checkAuth (req, res, next) {
// console.log(req.session);
// console.log('checkAuth ' + req.url);
// // don't serve /secure to those not logged in
// // you should add to this list, for each and every secure url
// // if (req.url === '/' && (!req.session || !req.session.authenticated)) {
// // res.render('unauthorised', { status: 403 });
// // return;
// // }
// next()
// }
app.use(session({ secret: 'example' }))
// app.use(checkAuth)
var url = 'http://192.168.178.109:1024/'
// Host the HTML File
app.get('/', function(req, res){
if(!req.session.name){
res.render('login')
return
}
request({
url: url,
json: true,
@ -39,4 +62,15 @@ app.get('/assets/*', function(req, res){
res.sendFile(__dirname + req.originalUrl)
})
app.post('/',function(req,res){
console.log(req);
if(req.body.name === 'Admin' && req.body.pass === '1234'){
req.session.name = req.body.name
res.end('success')
} else {
res.end('denied')
}
});
app.listen(8080)

39
package-lock.json generated
View file

@ -215,6 +215,11 @@
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
"crc": {
"version": "3.4.4",
"resolved": "https://registry.npmjs.org/crc/-/crc-3.4.4.tgz",
"integrity": "sha1-naHpgOO9RPxck79as9ozeNheRms="
},
"cryptiles": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/cryptiles/-/cryptiles-3.1.2.tgz",
@ -357,6 +362,22 @@
"promise": "7.3.1"
}
},
"express-session": {
"version": "1.15.6",
"resolved": "https://registry.npmjs.org/express-session/-/express-session-1.15.6.tgz",
"integrity": "sha512-r0nrHTCYtAMrFwZ0kBzZEXa1vtPVrw0dKvGSrKP4dahwBQ1BJpF2/y1Pp4sCD/0kvxV4zZeclyvfmw0B4RMJQA==",
"requires": {
"cookie": "0.3.1",
"cookie-signature": "1.0.6",
"crc": "3.4.4",
"debug": "2.6.9",
"depd": "1.1.2",
"on-headers": "1.0.1",
"parseurl": "1.3.2",
"uid-safe": "2.1.5",
"utils-merge": "1.0.1"
}
},
"extend": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz",
@ -705,6 +726,11 @@
"ee-first": "1.1.1"
}
},
"on-headers": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.1.tgz",
"integrity": "sha1-ko9dD0cNSTQmUepnlLCFfBAGk/c="
},
"once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
@ -769,6 +795,11 @@
"resolved": "https://registry.npmjs.org/qs/-/qs-6.5.1.tgz",
"integrity": "sha512-eRzhrN1WSINYCDCbrz796z37LOe3m5tmW7RQf6oBntukAG1nmovJvhnwHHRMAfeoItc1m2Hk02WER2aQ/iqs+A=="
},
"random-bytes": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
"integrity": "sha1-T2ih3Arli9P7lYSMMDJNt11kNgs="
},
"range-parser": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.0.tgz",
@ -966,6 +997,14 @@
"integrity": "sha1-bgkk1r2mta/jSeOabWMoUKD4grc=",
"optional": true
},
"uid-safe": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/uid-safe/-/uid-safe-2.1.5.tgz",
"integrity": "sha512-KPHm4VL5dDXKz01UuEd88Df+KzynaohSL9fBh096KWAxSKZQDI2uBrVqtvRM4rwrIrRRKsdLNML/lnaaVSRioA==",
"requires": {
"random-bytes": "1.0.0"
}
},
"unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",

View file

@ -16,6 +16,7 @@
"dependencies": {
"express": "^4.16.2",
"express-handlebars": "^3.0.0",
"express-session": "^1.15.6",
"request": "^2.83.0"
}
}

View file

@ -1,7 +1,7 @@
<html>
<head>
<title>Session Management in NodeJS using Express4.2</title>
<script src="../jquery-3.3.1.min.js"></script>
<script src="assets/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function(){
var name,pass;
@ -12,9 +12,9 @@ $(document).ready(function(){
* Perform some validation here.
*/
$.post(window.location.href, {name:name,pass:pass}, function(data){
if(data==='done')
if(data==='success')
{
window.location.href="/admin";
window.location.href="/";
}
});
});