Skip to content
Snippets Groups Projects
Commit 04e85508 authored by Elferink, A.S. (Amber)'s avatar Elferink, A.S. (Amber)
Browse files

Merge branch 'Amber' into 'master'

Amber

See merge request a.s.elferink/fastdiningshop!10
parents 2e520b6d 8ee6783e
No related branches found
No related tags found
No related merge requests found
Showing with 244 additions and 64 deletions
No preview for this file type
var sqlite3 = require('sqlite3').verbose();
//creates a new database
let db = new sqlite3.Database('fastdining.db', (err) => {
if (err) {
return console.error(err.message);
}
console.log('Connected to the in-memory SQlite database');
});
db.all("SELECT * FROM Persons", [], function(err, rows){
if(err)
{
return console.error(err.message);
}
console.log(rows);
});
//db.get als je een tupel wil krijgen
//geeft het terug in een javascript object
//row.name geeft naam
//db.all als je alle tupels wil in een array. Elk object is een element uit een array
//wacht tot alle queries klaar zijn en sluit de database dan af
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Close the database connection.');
});
......@@ -23,7 +23,10 @@ var addUserNameRouter = require('./routes/addUserName');
//database communicatie bestanden
var loadProducts = require('./routes/loadProducts');
var checkLoginRouter = require('./routes/checkLogin')
var checkLoginRouter = require('./routes/checkLogin');
var registerUserRouter = require('./routes/registerDatabase');
var profileRouter = require('./routes/profile');
var app = express();
......@@ -55,6 +58,9 @@ app.use('/register', registerRouter);
app.use('/api/addUserName', addUserNameRouter);
app.use('/api/checkLogin', checkLoginRouter);
app.use('/api/products', loadProducts);
app.use('/api/register', registerUserRouter);
app.use('/profile', profileRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
......
......@@ -71,7 +71,7 @@ var ProductBox = class {
}
else
{
textnode = document.createTextNode(quantity);
textnode = document.createTextNode("amount: " + quantity);
}
volumeEl.appendChild(textnode);
box.appendChild(volumeEl);
......
......@@ -47,7 +47,7 @@ function validate() {
function postLogin(callback, un, pw) {
$.ajax({
type: 'POST',
url: '/api/checkLogin',
url: '/api/checkLogin/login',
dataType: 'json',
data: {
"username": un,
......@@ -66,8 +66,9 @@ function postLogin(callback, un, pw) {
}
function giveAlert(returnValue) {
if (returnValue == true) {
alert("Login was successful");
if (returnValue.boolLoginCorrect == true) {
//since username must be unique for the database, it can be used for the url
window.location.replace("/profile/" + returnValue.currentuser);
}
else
{
......
console.log("navbar javascript loaded");
$('#logout').click(function () {
$.ajax({
type: 'GET',
url: '/api/checkLogin/logout',
dataType: 'text',
})//als deze asynchronous ajax call klaar is, is het of gefaald, of goed gegaan.
//als het goed is gegaan, callt hij de .done hieronder.
.done(function (data) {
//deze done functie logt het naar de javascript console en print het op de pagina als txt
//console.log('GET response:', JSON.stringify(data, "", 2));
window.location.replace("/");
alert(data); //gives "logout was succesful" if user was logged in, otherwise nothing.
})
//als het niet goed is gegaan, doet hij de fail hieronder
.fail(function (jqXHR, textStatus, err) {
console.log('AJAX error response:', textStatus);
});
});
\ No newline at end of file
......@@ -3,25 +3,61 @@ var sqlite3 = require('sqlite3').verbose();
var fs = require("fs");
var file = __dirname + "/../ConnectionJs/fastdining.db";
var exists = fs.existsSync(file);
var express = require('express');
var router = express.Router();
var app = express();
session = require('express-session');
app.use(session({
path: '/profile',
secret: '2C44-4D44-WppQ38S',
resave: true,
saveUninitialized: false,
duration: 30 * 60 * 1000, //set interaction for half an hour on login
activeDuration: 5 * 60 * 1000, //extend session for 5 mins with interaction
}));
/*If a get or post loads a page, for instance /myprofile, it wil look if the person is authorised to see it using this.
*/
/* GET home page. */
router.post('/', function (req, res) {
router.post('/login', function (req, res) {
//dit function deel is hetgene waarmee de callback(undefined, rows) wordt aangeroepen
//alles binnen deze functie doet hij pas nadat de callback is uitgevoerd. Als je res.send(data);
//dus onder de }); zet, dan krijg je undefined terug omdat de callback nog niet klaar was, toen hij was uitgevoerd
checkLoginWithDatabase(function(err, returnValues){
var data = returnValues;
res.send(data);
checkLoginWithDatabase(function(err, returnValues, username){
//login is correct
if(returnValues == true)
{
req.session.username
req.session.user = username;
res.send(
{"boolLoginCorrect": returnValues, "currentuser": username}
);
}
//login is not correct
else
{
res.send(returnValues);
}
},req.body);
});
router.get('/logout', function (req, res) {
console.log("I am logging out");
req.session.destroy();
res.send("logout success!");
});
module.exports = router;
......@@ -56,7 +92,8 @@ function checkLoginWithDatabase(callback, loginData){
return;
}
else if (row[i].password == loginData.password) {
callback(undefined, true);
callback(undefined, true, loginData.username);
return;
}
else {
......
var sqlite3 = require('sqlite3').verbose();
var fs = require("fs");
var file = __dirname + "/../ConnectionJs/fastdining.db";
var exists = fs.existsSync(file);
var express = require('express');
var router = express.Router();
var app = express();
session = require('express-session');
/*
router.get('/:username',auth, function(req, res, next) {
console.log(req.param('username'));
res.render('profile', {title: 'Profile Fast Dining', layout: 'layoutProfile'});
});*/
//authorises if the user may access the page
var auth = function (req, res, next) {
if(req.session && req.session.user === req.param('username'))
{
return next();
}
else
{
return res.sendStatus(401); //login session not active/unauthorised
}
};
router.get('/:username', auth, function (req, res) {
console.log(req.query);
res.render('profile', {title: 'Profile Fast Dining', layout: 'layoutProfile'});
});
module.exports = router;
var sqlite3 = require('sqlite3').verbose();
var fs = require("fs");
var file = __dirname + "/../ConnectionJs/fastdining.db";
var exists = fs.existsSync(file);
var express = require('express');
var router = express.Router();
router.post('/', function (req, res) {
req.check('email', 'Invalid email address').isEmail();
req.check('password', 'Invalid password').isLength({min: 4}).equals(req.body.confirmPassword);
addRegistryToDatabase(function(err, returnValues){
var data = returnValues;
res.send(data);
var errors = req.validationErrors();
if (errors){
req.session.error = errors;
req.session.succes = false;
} else {
req.session.succes = true;
}
},req.body);
});
module.exports = router;
function addRegistryToDatabase(callback, registerData) {
console.log("checking");
//creates a new database
let db = new sqlite3.Database(file, (err) => {
if (err) {
return console.error(err.message);
}
console.log('Connected to the database');
});
//selects correct tuple and checks if password is the same as the entered password.
db.serialize(function () {
// insert one row into the langs table
db.run(`INSERT INTO Persons(firstname, surname, username, password, emailaddress) VALUES(?, ?, ?, ?, ?)`, [registerData.firstname, registerData.surname, registerData.username, registerData.password, registerData.email], function (err) {
if (err) {
return console.log(err.message);
}
// get the last insert id
console.log(`A row has been inserted with rowid ${this.lastID}`);
});
});
db.all("SELECT * FROM Persons", function (err, row) {
for(var i = 0; i < row.length; i++) {
if (err) {
return callback(err);
}
if (row == undefined) {
//username not found in database
callback(undefined, false);
return;
}
else if (row[i].password == registerData.password) {
callback(undefined, true);
return;
}
else {
//password not correct
callback(undefined, false);
return;
}
}
//username not found in database row.length = 0
callback(undefined, false);
return;
});
//wacht tot alle queries klaar zijn en sluit de database dan af
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Close the database connection.');
});
}
module.exports = router;
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Fast Dining - {{title}}</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
<link rel='stylesheet' href='/stylesheets/style.css' /><!-- dit zo laten staan, de express-handlebars zoekt hem automatisch in de stylesheets map-->
</head>
<body>
{{>header}} <!-- dit commando van express-handlebars zoekt by default in partials folder-->
<main class="content">
{{{body}}}
</main>
<!-- basis javascript van bootstrap-->
<script src="/libraries/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script type=text/javascript src="/javascripts/profile.js"></script>
</body>
</html>
\ No newline at end of file
......@@ -5,12 +5,7 @@
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
<link rel='stylesheet' href='stylesheets/style.css' /><!-- dit zo laten staan, de express-handlebars zoekt hem automatisch in de stylesheets map-->
<script type="text/javascript" src="libraries/flot/excanvas.min.js"></script>
<script type="text/javascript" src="libraries/flot/jquery.js"></script>
<script type="text/javascript" src="libraries/flot/jquery.min.js"></script>
<script type="text/javascript" src="libraries/flot/jquery.flot.js"></script>
<script type="text/javascript" src="libraries/flot/jquery.flot.axislabels.js"></script>
<script type="text/javascript" src="libraries/flot/jquery.flot.stack.js"></script>
<script type="text/javascript" src="libraries/jquery.min.js"></script>
</head>
<body>
......@@ -19,9 +14,6 @@
{{{body}}}
<!-- basis javascript van bootstrap-->
<script src="http://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script type=text/javascript src="javascripts/register.js"></script>
......
......@@ -16,7 +16,7 @@
</ul>
</section>
{{/if}} --->
<div name="login">
<section name="login">
<div class="input">
<label>Username</label>
<input type="text" id="username" name="username" placeholder="your login name">
......@@ -35,5 +35,7 @@
</div> --->
<button onclick="validate()">Log in</button>
</div>
<br>
<a href="/register">Or click here to register</a>
</section>
<!--- {{/if}} --->
\ No newline at end of file
<header>
<img src="images/website-header.png" alt="layout of ingedrients">
<img src="/images/website-header.png" alt="layout of ingedrients">
</header>
<nav>
......@@ -19,17 +19,18 @@
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><i class="fas fa-user"></i> User<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">My Profile</a></li>
<li><a href="/login">My Profile</a></li>
<li><a href="#">Order History</a></li>
<li><a href="#"></a>Blabla</li>
<li role="separator" class="divider"></li>
<li><a href="#">Log out</a></li>
<li><a id="logout">Log out</a></li>
</ul>
</li>
</ul>
</div>
</nav>
<script src="/libraries/jquery.min.js"></script>
<script type=text/javascript src="/javascripts/navbar.js"></script>
<!--
<nav class="navbar navbar-default">
<div class="container-fluid">
......
<p>Hi there</p>
\ No newline at end of file
......@@ -16,7 +16,7 @@
</ul>
</section>
{{/if}}
<form name="login" action="/register/addUsername" method="post">
<form name="login" action="/api/register" method="post">
<div class="input">
<label>First Name</label>
<input type="text" id="firstname" name="firstname" placeholder="your first name">
......@@ -42,6 +42,6 @@
<input type="password" id="confirmPassword" name="confirmPassword" placeholder="confirm your password">
</div>
<button type="submit" onclick="validate()">Sign up</button>
<button type="submit" >Sign up</button>
</form>
{{/if}}
\ No newline at end of file
......@@ -13,8 +13,9 @@
</section>-->
<h1>{{title}}</h1>
<h2>Search for products</h2>
<input type="text" id="productSearch" placeholder="search for products">
<br>
<section id="productBox" class="row"></section>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment