From 4eef658e328435108a9bb1308c459e647bd5e9f3 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 20 Feb 2018 02:50:03 +0100 Subject: [PATCH] Fix Login --- index.js | 37 ++++++++++++++----------------------- views/login.hbs | 6 ++---- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 109fbba..dcbc130 100644 --- a/index.js +++ b/index.js @@ -1,28 +1,17 @@ var app = require('express')(), session = require('express-session'), http = require('http').Server(app), + bodyParser = require('body-parser'), 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) +app.use(session({ resave: true, saveUninitialized: false, secret: '62gzrhufijw' })) +app.use(bodyParser.json()) +app.use(bodyParser.urlencoded({extended: true})) var url = 'http://192.168.178.109:1024/' @@ -37,8 +26,8 @@ app.get('/', function(req, res){ url: url, json: true, auth: { - user: 'Simon', - pass: 'password' + user: req.session.name, + pass: req.session.pass } }, function(error, response, body){ var data = [] @@ -63,14 +52,16 @@ app.get('/assets/*', function(req, res){ }) app.post('/',function(req,res){ - console.log(req); - - if(req.body.name === 'Admin' && req.body.pass === '1234'){ + if( + req.body.name === 'Simon' && req.body.pass === 'password' || + req.body.name === 'USER2' && req.body.pass === 'rfcgvhbßä06' + ){ req.session.name = req.body.name - res.end('success') + req.session.pass = req.body.pass + res.end('true') } else { - res.end('denied') + res.end() } - }); + }) app.listen(8080) diff --git a/views/login.hbs b/views/login.hbs index 5d64b32..0e75c75 100644 --- a/views/login.hbs +++ b/views/login.hbs @@ -12,10 +12,8 @@ $(document).ready(function(){ * Perform some validation here. */ $.post(window.location.href, {name:name,pass:pass}, function(data){ - if(data==='success') - { - window.location.href="/"; - } + if(data === 'true') + window.location.href= '/'; }); }); });