Skip to content

Commit

Permalink
.env added and .env example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwill9999 committed Mar 16, 2019
1 parent ed5ee89 commit 155d9b2
Show file tree
Hide file tree
Showing 6 changed files with 3,008 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JWT_SECRET=<add secret key here>
DATABASE_CONNECTION=<add mongodb connection string here>
36 changes: 20 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ const mongoose = require('mongoose');
/*******************************************************************************/

mongoose.Promise = global.Promise;
try {
mongoose.connect(process.env.DATABASE_CONNECTION);
console.log('Connected to Mongodb :- DataBase listening on Port 27017');
} catch(e){
console.log('ERROR :- Could not connect to your MongoDb DataBase');
}



/**
* Mongoose Database connection
* @params connection {string}
* @returns {promise}
*/
mongoose.connect(process.env.DATABASE_CONNECTION, { userNewUrlParser: true }).then(
() => { console.log("\x1b[32m%s\x1b[0m", 'Connected to Mongodb :- DataBase listening on Port 27017'); },
err => { console.log("\x1b[31m%s\x1b[0m", 'ERROR :- Could not connect to your MongoDb DataBase', err); }
);


const app = express();
Expand All @@ -44,20 +48,20 @@ app.use('/users', users);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
let err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
74 changes: 37 additions & 37 deletions controllers/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,51 @@ const jwt = require('jwt-simple');

//create jwt-token and set subject, timestamp and secret
function createJwtToken(user) {
const timestamp = new Date().getDate();
return jwt.encode({ sub: user.id, iat: timestamp }, process.env.JWT_SECRET)
const timestamp = new Date().getDate();
return jwt.encode({ sub: user.id, iat: timestamp }, process.env.JWT_SECRET)
};


//authorized just need to issue a jwt_token
exports.signin = (req, res, next) => {
res.send({ jwt_web_token: createJwtToken(req.user) })
res.send({ jwt_web_token: createJwtToken(req.user) })
}

exports.signup = (req, res, next) => {
const email = req.body.email;
const password = req.body.password;

if (!email || !password) {
return res.status(422).send({ error: 'Email and Password are required' })
}

/****************add email validation********************/


//does user with email exist?
User.findOne({ email }, (err, existingUser) => {
if (err) {
return next(err);
}

//if email already exists return error message
if (existingUser) {
return res.send(422, { error: 'Email already exists' })
}
//if new user create new User and return JWT_TOKEN
const user = new User({
email: email,
password: password
});
user.save((err) => {
if (err) {
return next(err);
}
res.send({ jwt_web_token: createJwtToken(user) });
});

});
const email = req.body.email;
const password = req.body.password;

if (!email || !password) {
return res.status(422).send({ error: 'Email and Password are required' })

}

/****************add email validation********************/


//does user with email exist?
User.findOne({ email }, (err, existingUser) => {
if (err) {
return next(err);
}

//if email already exists return error message
if (existingUser) {
return res.send(422, { error: 'Email already exists' })
}
//if new user create new User and return JWT_TOKEN
const user = new User({
email: email,
password: password
});
user.save((err) => {
if (err) {
return next(err);
}
res.send({ jwt_web_token: createJwtToken(user) });
});

});

};

Loading

0 comments on commit 155d9b2

Please sign in to comment.