Skip to content

Commit

Permalink
fixed sign up logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EmBla312 committed Jun 11, 2020
1 parent 3bfc826 commit 1a38867
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ app.use(express.static(path.join(__dirname, 'pages'))); //this sets a static pat

app.listen(PORT, () => {
console.log('Server is starting on PORT, ', PORT);




})
/****************************************** */

Expand All @@ -43,30 +39,30 @@ app.post('/signup.html', (req, res) => {
var db = client.db(dbname); //get database object

var users_collection = db.collection('users'); //get colleciton in database

users_collection.find({ username: req.body.username }).toArray((err, res) => {
if(err) throw err;
console.log(res); //res = an array of all objects/docs in db

var usersCursor = users_collection.find({
username: req.body.username
});

usersCursor.forEach(document => {
//if a user already exists with the sign up username, don't create a new user
if(document.username === req.body.username) {
console.log('sign up unsuccessful');
}
else {
//if there is no existing user with the signup username
if(res.length < 1) {
var userdoc = {
username: req.body.username,
password: req.body.password,
email: req.body.email,
createDateTime: date,
lastlogin: date
}

users_collection.insertOne(userdoc);
console.log('sign up successful: user logged');
}
//if user already exists
else {
console.log("username already exists: failed sign up");
}
});

}
});
});
Expand All @@ -87,7 +83,7 @@ app.post('/signin.html', (req, res) => {
});

usersCursor.forEach(document => {
if(document.password === req.body.password) {
if (document.password === req.body.password) {
console.log('login successful');
}
else {
Expand Down

0 comments on commit 1a38867

Please sign in to comment.