Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added login_server #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
1 change: 1 addition & 0 deletions login_server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGODB_URI=mongodb+srv://MaissaJ:<Maissa123>@cluster0.yi3ha.mongodb.net/UserDB?retryWrites=true&w=majority
158 changes: 158 additions & 0 deletions login_server/api/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
const express = require('express');
const router = express.Router();

//mongodb user model
const User = require('./../models/User');

// Password handler
const bcrypt = require('bcrypt');
//signup
router.post('/signup'), (req, res) => {
let {name, email, password, dateOfBirth} = req.body;
name = name.trim();
email = email.trim();
password = password.trim();
dateOfBirth = dateOfBirth.trim();

if (name == "" || email == "" || password == "" || dateOfBirth == "") {
res.json ({
status: "FAILED",
message: "Empty input fields!"
});
} else if (!/^[a-zA-Z ]*$/.test(name)) {
res.json ({
status: "FAILED",
message: "Invalid name entered"
})

} else if (!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email)) {
res.json ({
status: "FAILED",
message: "Invalid email entered"
})

} else if (!new Date(dateOfBirth).getTime()) {
res.json ({
status: "FAILED",
message: "Invalid date of birth entered"
})
} else if (password.length < 8 ) {
res.json ({
status: "FAILED",
message: "Password is too short!"
})
} else {
//checking if user already exists
User.find ({email}).then(result => {
if (result.length) {
//A user already exists
res.json ({
status: "FAILED",
message: "User with the provided email already exists"
})
} else {
// Try to create new user

// password handling
const saltRounds = 10;
bcrypt.hash(password, saltRounds).then(hashedPassword => {
const newUser = new User({
name,
email,
password: hashedPassword,
dateOfBirth
});

newUser.save().then(result => {
res.json({
status: "SUCCESS",
message: "signup successful",
data: result,
})
})
.catch(err => {
res.json({
status: "FAILED",
message: "An error occured while saving user account!"
})
})

})
.catch(err => {
res.json({
status: "FAILED",
message: "An error occured while hashing password!"
})
})
}

}).catch(err => {
console.log(err);
res.json ({
status: "FAILED",
message: "An error occured while checking for existing user!"
})
})
}


}

//signin
router.post ('/signin', (req, res) => {
let {email, password} = req.body;
email = email.trim();
password = password.trim();

if (email == "" || password =="") {
res.json ({
status: "FAILED",
message: "Empty credentials supplied"
})
} else {
// Check if user exist
User.find({email})
.then(data => {
if (data.length) {
//user exists

const hashedPassword = data[0].password;
bcrypt.compare(password, hashedPassword).then(result => {
if (result) {
// PAssword match
res.json ({
status: "SUCCESS",
message: "Signin successful",
data: data
})
} else {
res.json ({
status: "FAILED",
message: "Invalid password entered!"
})
}
})
.catch (err => {
res.json ({
status: "FAILED",
message: "An error occured while comparing passwords"
})
})
} else {
res.json ({
status: "FAILED",
message: "Invalid credentials entered!"
})
}
})
.catch(err => {
res.json ({
status: "FAILED",
message: "An error occured while checking for existing user"
})
})
}

})

module.exports = router;
13 changes: 13 additions & 0 deletions login_server/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require('dotenv').config();
const mongoose = require('mongoose');

mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifieldTopology: true,
})
.then(()=> {
console.log("DB Connected");
})

.catch((err) => console.log(err));
13 changes: 13 additions & 0 deletions login_server/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const mongoose = require('mongoose');
const schema = mongoose.schema;

const UserSchema = new Schema ({
name: String,
email: String,
password: String,
dateOfBirth: Date
});

const User = mongoose.model ('User', UserSchema);

module.exports = User;
12 changes: 12 additions & 0 deletions login_server/node_modules/.bin/detect-libc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions login_server/node_modules/.bin/detect-libc.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions login_server/node_modules/.bin/detect-libc.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions login_server/node_modules/.bin/is-ci

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions login_server/node_modules/.bin/is-ci.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions login_server/node_modules/.bin/is-ci.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions login_server/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions login_server/node_modules/.bin/mime.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions login_server/node_modules/.bin/mime.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions login_server/node_modules/.bin/mkdirp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading