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

implemented registration and login for the user #94

Merged
merged 7 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
"author": "Aashutosh Rathi",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-fileupload": "^1.1.4",
"express-session": "^1.16.1",
"express-validator": "^5.3.1",
"helmet": "^3.16.0",
"mongoose": "^5.4.19"
"mongoose": "^5.4.19",
"passport": "^0.4.0",
"passport-local": "^1.0.0"
},
"devDependencies": {
"nodemon": "^1.18.10"
Expand Down
108 changes: 108 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const compile = require('./routes/api/compile');
const zip = require('./routes/api/zip');
const generate = require('./routes/api/generate');
const upload = require('express-fileupload');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const passport = require('passport');
const localStrategy = require('passport-local').Strategy;
const expressValidator = require('express-validator');
const expressSession = require('express-session');

const User = require('./models/User');
const app = express();
var fs = require('fs');

Expand All @@ -23,9 +31,63 @@ mongoose
.catch(err => console.log(err));

app.use(upload());

// Body Parser

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(expressValidator());

app.use(passport.initialize());
app.use(passport.session());

app.use(
expressSession({
secret: 'ilovecoding',
saveUninitialized: true,
resave: true
})
);

app.use(express.static('public'));
app.set('view engine', 'ejs');

passport.use(
new localStrategy(
{
usernameField: 'email'
},
function(email, password, done) {
User.findOne({ email: email }, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
bcrypt.compare(password, user.password, function(err, matched) {
if (err) {
return done(err);
} else if (matched) {
return done(null, user);
} else {
return done(null, false, { message: 'incorrect password' });
}
});
});
}
)
);
passport.serializeUser(function(user, done) {
return done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
return done(err, user);
});
});

app.get('/', (req, res) => {
res.render('pages/landing');
});
Expand Down Expand Up @@ -57,7 +119,53 @@ app.post('/api/upload', function(req, res) {
res.send('Sorry please select the file to upload');
}
});
app.get('/register', function(req, res) {
res.render('pages/signup', {
errors: req.session.errors
});
//resetting session properties to null
req.session.errors = null;
});
app.post('/register', function(req, res) {
req.check('firstName', 'First name is required').notEmpty();
req.check('lastName', 'Last Name required').notEmpty();
req.check('email', 'Invalid email address').isEmail();
req.check('password', 'Password is invalid must be of length 6').isLength({ min: 6 });
req.check('password', 'Passwords are not matching').equals(req.body.confirmPassword);

let errors = req.validationErrors();
if (errors) {
req.session.errors = errors;
res.redirect('/register');
} else {
const newUser = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: req.body.password
});
bcrypt.genSalt(10, (error, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
newUser.password = hash;
newUser
.save()
.then(savedUser => {
res.redirect('/login');
})
.catch(error => console.log(error));
});
});
}
});
app.get('/login', (req, res) => {
res.render('pages/login');
});
app.post('/login', (req, res, done) => {
passport.authenticate('local', {
failureRedirect: '/login',
successRedirect: '/'
})(req, res, done);
});
//Compile, generate and zip routes
app.use('/api/compile', compile);
app.use('/api/zip', zip);
Expand Down
22 changes: 22 additions & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const userSchema = new Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});

module.exports = mongoose.model('users', userSchema);
14 changes: 6 additions & 8 deletions views/components/header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
</style>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<nav class="navbar navbar-expand-md bg-dark navbar-light fixed-top">
<div class="container">
<a href="index.html" class="navbar-brand text-white">Test Case Generator</a>
<button class="navbar-toggler bg-secondary collapsed" data-toggle="collapse" data-target="#navbarCollapse">
<button class="navbar-toggler bg-secondary" data-toggle="collapse" data-target="#navbarCollapse">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse " id="navbarCollapse">
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a href="#" class="nav-link text-white">About</a>
Expand All @@ -23,21 +23,19 @@
<a href="/upload-file" class="nav-link text-white">Upload-File</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-white">Login</a>
<a href="/login" class="nav-link text-white">Login</a>
</li>
<li class="nav-item">
<a href="/api/compile" class="nav-link text-white">Compile-File</a>
</li>
<li class="nav-item">
<a href="/api/zip" class="nav-link text-white">Generate Test Cases</a>
<li class="nav-item">
<a href="#" class="nav-link text-white">Upload-File</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link text-white login">Login</a>
<a href="/register" class="nav-link text-white login">Register</a>
</li>
</ul>
</div>
</div>
</nav>

File renamed without changes.
30 changes: 30 additions & 0 deletions views/pages/login.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Login</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="card mt-5 mx-auto">
<div class="card-header text-center">Login</div>
<div class="card-body">
<form action="/login" method="post" enctype="application/x-www-form-urlencoded">
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" class="form-control">
</div>
<button type="submit" class="btn btn-primary btn-block">Login</button>
</form>


</div>

</div>
</body>
54 changes: 54 additions & 0 deletions views/pages/signup.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>Sign Up</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<% include ../components/header %>
</head>

<body>
<div class="card mx-auto mt-5 pt-5">
<% if(errors) { %>
<% errors.forEach(function(errors){ %>
<div class="alert alert-danger"><%= errors.msg %></div>
<% }) %>
<% } %>
<div class="card-header text-center text-capitalize">Register with us</div>
<div class="card-body">
<form action="/register" method="post" enctype="application/x-www-form-urlencoded">
<div class="form-row">
<div class="col-md-6">
<div class="form-group">
<label for="firstName">First Name</label>
<input type="text" id="firstName" name="firstName" placeholder="Enter first Name" class="form-control">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" placeholder="Enter last Name" class="form-control">
</div>
</div>
</div>

<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email"id="email" placeholder="Enter your email" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Enter your password" class="form-control">
</div>
<div class="form-group">
<label for="confirmPassword">Confirm Password</label>
<input type="password" name="confirmPassword" id="confirmPassword" class="form-control" placeholder="Confirm your password">
</div>
<button type="submit" class="btn btn-primary btn-block">Register</button>
</form>
</div>
</div>
</body>
</html>