Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions lib/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
//Returns MD5 hash of given string input
function hash(str)
{
var hash = crypto
.createHash("md5")
.update(str)
.digest('hex');
console.log(hash);
var hash = crypto.createHash("md5").update(str).digest('hex');
return hash;
}

//The authentication function
var authenticate = function()
{
var self = this;

self.login = function(req,res)
{
var ses = req.session;
if(ses.logged == 1)
if(ses.logged == 1) //In case session variable set, redirect to homepage
{
if(ses.user_type == 1)
if(ses.user_type == 1) //If a buyer
res.redirect('/catalogue/');
else
else //If a seller
res.redirect('/inventory/')
}
else
else //Otherwise render login page
{
console.log("I was here");
return res.render('login',{error : ""});
}
};

self.signup = function(req,res)
self.signup = function(req,res) //Signup Action
{
var username = req.body.username;
var password = hash(req.body.password);
Expand All @@ -38,18 +35,17 @@ var authenticate = function()
var email = req.body.email;
var user_type = req.body.user_type;

console.log(htmlspecialchars(user_type));
//Build Insert Query
var string = 'INSERT INTO `User`(username,password,first_name,last_name,email,user_type) VALUES ('
+'"'+htmlspecialchars(username)+'"'
+',"'+htmlspecialchars(password)+'"'
+',"'+htmlspecialchars(first_name)+'"'
+',"'+htmlspecialchars(last_name)+'"'
+',"'+htmlspecialchars(email)+'"'
+','+user_type+')';
console.log(string);

connection.query(string,function(err,row,field)
{
console.log(err);
if(err==null)
return res.render('login',{error : "Sign Up Successfull"});
else
Expand All @@ -65,7 +61,7 @@ var authenticate = function()
if(ses.user_type == 1)
res.redirect('/catalogue/');
else
res.redirect('/inventory/')
res.redirect('/inventory/');
}
else
{
Expand All @@ -82,7 +78,7 @@ var authenticate = function()
ses.username = username;
ses.first_name = rows[0].first_name;
ses.last_name = rows[0].last_name;
ses.email = rows[0].email;
ses.email = rows[0].email;
ses.user_id = rows[0].id;
ses.address = rows[0].address;
ses.contact_no = rows[0].contact_no;
Expand Down Expand Up @@ -114,4 +110,4 @@ var authenticate = function()

};

module.exports = authenticate
module.exports = authenticate
9 changes: 5 additions & 4 deletions lib/buyer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//This is the buyer library and includes viewCart,addToCart and deleteFromCart
var buyer = function()
{
var self = this;
Expand Down Expand Up @@ -35,7 +36,7 @@ var buyer = function()
detail.push(item);
}
counter++;

if(counter == rows.length)
{
console.log(detail);
Expand All @@ -62,7 +63,7 @@ var buyer = function()
{
var user = ses.user_id;
var item = req.body.itemId;

connection.query('INSERT INTO CartItem (item,buyer) values ('+
htmlspecialchars(item)+','+
user+')',function(err,rows,field)
Expand All @@ -71,7 +72,7 @@ var buyer = function()
return res.send("Successfully Added Item to Cart");
else
return res.send(err);
});
});
}
else
{
Expand Down Expand Up @@ -104,4 +105,4 @@ var buyer = function()
};
};

module.exports = buyer;
module.exports = buyer;
13 changes: 7 additions & 6 deletions lib/checkout.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//This is the checkout library and provides function for checkout and checkoutOrder
var checkout = function()
{
var self = this;
Expand All @@ -19,7 +20,7 @@ var checkout = function()

self.checkoutOrder = function(req,res)
{
var ses = req.session;
var ses = req.session;
if(ses.logged == 1)
{
var user = new Object();
Expand All @@ -36,7 +37,7 @@ var checkout = function()
ses.address = req.body.address;
user.address = ses.address;
req.session = ses;

//modified contact_no
connection.query('UPDATE `User` SET `contact_no` = "'+contact_no+'" WHERE `id` = '+ses.user_id,function(err2,rows2,field2)
{
Expand Down Expand Up @@ -90,7 +91,7 @@ var checkout = function()
res.render('checkout',{user:user,error : "Couldn't complete order. Please try Again Later"});
}
});

}
else
{
Expand All @@ -101,7 +102,7 @@ var checkout = function()
res.redirect('/catalogue/');
}
}
});
});
}
}
else
Expand All @@ -121,7 +122,7 @@ var checkout = function()
else
{
res.render('checkout',{user:user,error : "kkCouldn't retrieve items. Please Try again later."});
}
}
});
//get all items
//delete all items
Expand All @@ -148,4 +149,4 @@ var checkout = function()
};
};

module.exports = checkout
module.exports = checkout