Browse Source

Implemented a login page for the admin

pull/4/head
jrtechs 6 years ago
parent
commit
446b0d1ebf
11 changed files with 138 additions and 54 deletions
  1. +4
    -6
      README.md
  2. +9
    -2
      admin/admin.js
  3. +18
    -13
      admin/login.html
  4. +37
    -7
      admin/login.js
  5. +3
    -1
      includes/404.html
  6. +0
    -1
      includes/includes.js
  7. +0
    -1
      posts/singlePost.js
  8. +3
    -12
      server.js
  9. +1
    -4
      sidebar/categoriesSideBar.js
  10. +58
    -5
      utils/sql.js
  11. +5
    -2
      utils/utils.js

+ 4
- 6
README.md View File

@ -10,12 +10,9 @@ use blog_name;
create table users(
user_id mediumint unsigned not null AUTO_INCREMENT,
first_name varchar(20) not null,
last_name varchar(40) not null,
user_name varchar(60) not null,
pass char(40) not null,
registration_date datetime not null,
admin boolean not null,
password char(64) not null,
salt char(64) not null,
primary key(user_id)
);
@ -49,11 +46,12 @@ grant all on blog_name.* to blog_user@localhost identified by "password";
## Node Dependencies
```bash
npm install express
npm install client-sessions
npm install express-sessions
npm install mysql
npm install sanitizer
npm install promise
npm install markdown
npm install markdown-to-html -g
npm install highlight
npm install crypto
```

+ 9
- 2
admin/admin.js View File

@ -1,5 +1,6 @@
const utils = require('../utils/utils.js');
var Promise = require('promise');
var session = require('client-sessions');
module.exports=
{
@ -18,7 +19,7 @@ module.exports=
{
if(request.session && request.session.user)
{
console.log("user logged in");
utils.getPostData(request).then(function (postData)
{
return require("../admin/newPost.js").main(result, postData);
@ -34,7 +35,13 @@ module.exports=
else
{
//login page
return require("../admin/login.js").main(result, request);
require("../admin/login.js").main(result, request).then(function()
{
resolve();
}).catch(function(err)
{
console.log(err);
})
}
});

+ 18
- 13
admin/login.html View File

@ -1,14 +1,19 @@
<div class="w3-container w3-dark-grey">
<h2>Login</h2>
</div>
<div class="w3-col 18 s12">
<div class="w3-card-4 w3-margin w3-white">
<div class="w3-container w3-dark-grey">
<h2>Login</h2>
</div>
<form class="w3-container">
<p>
<label class="w3-text-white"><b>User Name</b></label>
<input class="w3-input w3-border w3-light-grey" type="text"></p>
<p>
<label class="w3-text-white"><b>Password</b></label>
<input class="w3-input w3-border w3-light-grey" type="password"></p>
<p>
<button class="w3-btn w3-dark-grey">Login</button></p>
</form>
<form action="/admin/" method ="post" class="w3-container">
<p>
<label class="w3-text-black"><b>User Name</b></label>
<input class="w3-input w3-border w3-light-grey" type="text" name="username"></p>
<p>
<label class="w3-text-black"><b>Password</b></label>
<input class="w3-input w3-border w3-light-grey" type="password" name="password"></p>
<p>
<button class="w3-btn w3-dark-grey">Login</button></p>
</form>
</div>
</div>
</div>

+ 37
- 7
admin/login.js View File

@ -1,18 +1,48 @@
const utils = require('../utils/utils.js');
const Promise = require('promise');
const sql = require('../utils/sql');
var processLogin = function(result, request)
{
return new Promise(function(resolve, reject)
{
utils.getPostData(request).then(function(postData)
{
return sql.checkLogin(postData);
}).then(function(loginResult)
{
if(loginResult.pass)
{
request.session.user = loginResult.user;
result.write("<meta http-equiv=\"refresh\" content=\"0\">");
}
resolve();
}).catch(function(err)
{
console.log(err);
resolve();
})
});
};
module.exports=
{
main: function(result, request)
{
utils.include(result, './admin/login.html');
return this.processLogin(result, request);
},
processLogin: function(result, request)
{
result.write("<div class=\"w3-col l8 s12\">");
return new Promise(function(resolve, reject)
{
resolve();
utils.include(result, './admin/login.html').then(function()
{
return require("../sidebar/sidebar.js").main(result);
}).then(function()
{
return processLogin(result, request);
}).then(function()
{
resolve();
})
});
}
},
};

+ 3
- 1
includes/404.html View File

@ -1,2 +1,4 @@
<h1 class="w3-center">Page Not Found</h1>
<center><img src="/img/website/404.jpg" alt="Page not found" width="70%" /></center>
<div class="w3-center">
<img src="/img/website/404.jpg" alt="Page not found" width="70%" />
</div>

+ 0
- 1
includes/includes.js View File

@ -33,7 +33,6 @@ module.exports =
{
return new Promise(function(resolve, reject)
{
console.log(FOOTER_FILE);
utils.include(result, FOOTER_FILE).then(function()
{
result.end();

+ 0
- 1
posts/singlePost.js View File

@ -39,7 +39,6 @@ module.exports=
html = html.split("<code>").join("<pre><code>");
html = html.split("</code>").join("</code></pre>");
res.write(html);
console.log(html);
}
catch(ex)
{

+ 3
- 12
server.js View File

@ -11,18 +11,13 @@ const url = require('url');
var express = require("express");
var session = require('client-sessions');
var session = require('express-session');
const includes = require('./includes/includes.js');
var app = express();
app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));
app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 6000000 }}));
app.use(function(request, res)
{
@ -44,8 +39,6 @@ app.use(function(request, res)
var urlSplit = filename.split("/");
console.log(urlSplit);
if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "./posts/category.js";
@ -55,8 +48,6 @@ app.use(function(request, res)
else
file = "./posts/posts.js";
console.log(file);
includes.printHeader(res).then(function()
{
return require(file).main(res, filename, request);
@ -65,7 +56,7 @@ app.use(function(request, res)
return includes.printFooter(res);
}).then(function()
{
console.log("fin"); //for debugging
//console.log("fin"); //for debugging
})
}

+ 1
- 4
sidebar/categoriesSideBar.js View File

@ -12,7 +12,6 @@ module.exports=
*/
main: function(res)
{
console.log("sidebar called");
return new Promise(function(resolve, reject)
{
res.write("<div class=\"w3-card w3-margin\">");
@ -23,18 +22,16 @@ module.exports=
sql.getCategories().then(function(categories)
{
console.log(categories[0].name);
categories.forEach(function(cat)
{
//res.write(cat.name);
console.log(cat);
res.write("<li class=\"w3-padding-16\">");
res.write("<a href='/category/" + cat.url + "'><span class=\"w3-large\">" + cat.name + "</span><br></a>");
res.write("</li>");
});
res.write("</ul>");
res.write("</ul></div>");
resolve();
})

+ 58
- 5
utils/sql.js View File

@ -2,7 +2,11 @@ const mysql = require('mysql');
const sanitizer = require('sanitizer');
var Promise = require('promise');
const Promise = require('promise');
const crypto = require('crypto');
const qs = require('querystring');
const con = mysql.createConnection({
host: "localhost",
@ -13,7 +17,6 @@ const con = mysql.createConnection({
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
/**
@ -139,6 +142,7 @@ module.exports=
resolve(0);
}
});
resolve(0);
});
},
@ -190,12 +194,61 @@ module.exports=
});
});
},
getPage: function(name)
/**
* Function which checks to see if a user successfully logged in based on
* the post data which they sent
*
* @param postData the post data
* @return {*|Promise} a json object with {pass: , user: }
* the pass is whether or not they logged in successfully and the user is
* the username they successfully logged in with
*/
checkLogin: function(postData)
{
var post = qs.parse(postData);
return new Promise(function(resolve, reject)
{
var q = "";
});
var result = Object();
result.pass = false;
if(post.username && post.password)
{
var cleanName = sanitizer.sanitize(post.username);
var cleanPassword = sanitizer.sanitize(post.password);
var getSalt = "select * from users where user_name='" + cleanName + "'";
fetch(getSalt).then(function(saltResult)
{
if(saltResult.length == 1)
{
var hashedPassword = crypto.createHash('sha256')
.update(cleanPassword + saltResult[0].salt)
.digest('hex');
if(saltResult[0].password === hashedPassword)
{
//yay!
result.pass = true;
result.user = cleanName;
resolve(result);
}
else
{
//wrong password
resolve(result)
}
}
else
{
//incorrect username
resolve(result);
}
})
}
else
{
//no login attempts were made
resolve(result);
}
});
}
};

+ 5
- 2
utils/utils.js View File

@ -32,6 +32,11 @@ module.exports=
});
},
/**
* Method which return the contents of a file as a string
* @param fileName
* @return {*}
*/
getFileContents: function(fileName)
{
try
@ -71,12 +76,10 @@ module.exports=
req.connection.destroy();
reject();
}
});
req.on('end', function ()
{
console.log(body);
resolve(body);
});
}

Loading…
Cancel
Save