Skip to content

Commit

Permalink
Modification for the signup
Browse files Browse the repository at this point in the history
  • Loading branch information
amra12 committed May 13, 2018
1 parent f2c2b9e commit 1f3ad7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion config/database.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
'secret': 'hakunamatata', // to encode are javascript web token (jwt)
'secret': 'hakunamatata',
'database': 'mongodb://localhost/objectivo' //access to mongo db
};
26 changes: 11 additions & 15 deletions controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,26 @@ class UserController {

this.basePath = '/user';
//POST: for creating user : provided url + '/user/'
apiRouter.post(this.basePath + "/", this.signUp.bind(this));
// GET: for signing out The user : provided url + '/api/user/123abc' where 123abc is The id of The user to sign out
apiRouter.get(this.basePath + "/signOut", this.signOut.bind(this));
apiRouter.post(this.basePath + "/signUp", this.signUp.bind(this));


// POST: for Singing in : provided url + '/api/user/login'
apiRouter.post(this.basePath + "/signIn", this.signIn.bind(this));
}

signOut(req, res) {

var name = req.params.name;
return res.status(200).json({success: true, msg: 'Signed out successfully'});

}

signUp(req, res) {
console.log('user controller signUp')
console.log('user controller signUp' )

if (!req.body.email || !req.body.password || !req.body.name) {
if (!req.body.Email || !req.body.Password || !req.body.Name) {
res.status(400).json({success: false, msg: 'Please pass the information'});

} else {
var newUser = new User({
name: req.body.name,
email: req.body.email,
password: req.body.password,
Name: req.body.Name,
Email: req.body.Email,
Password: req.body.Password,

});
// save the user
Expand All @@ -50,15 +45,16 @@ class UserController {

signIn(req, res) {

console.log('in log in + ' + req.body.Email)
User.findOne({
email: req.body.email
Email: req.body.Email
}, function (err, user) {
if (err) res.status(502).json({success: false, msg: err.message});

if (!user) {
return res.status(403).json({success: false, msg: 'Sign in failed. Email not found.'});
} else {
user.comparePassword(req.body.password, function (err, isMatch) {
user.comparePassword(req.body.Password, function (err, isMatch) {
if (isMatch && !err) {
res.status(200).json({success: true, name: user.name});
} else {
Expand Down
16 changes: 8 additions & 8 deletions model/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ var bcrypt = require('bcrypt');

// set up a mongoose model for our database note: the model has already implemented functions which i will use
var UserSchema = new Schema({
name: {
Name: {
type: String,
required: true
},
email: {
Email: {
type: String,
unique : true,
required: true
},
password: {
Password: {
type: String,
required: true
},
savedPhrases:
savedVocab:
{
type: [String]
},
Expand All @@ -27,16 +27,16 @@ var UserSchema = new Schema({

UserSchema.pre('save', function (next) {
var user = this;
if (this.isModified('password') || this.isNew) {
if (this.isModified('Password') || this.isNew) {
bcrypt.genSalt(10, function (err, salt) {
if (err) {
return next(err);
}
bcrypt.hash(user.password, salt, function (err, hash) {
bcrypt.hash(user.Password, salt, function (err, hash) {
if (err) {
return next(err);
}
user.password = hash;
user.Password = hash;
next();
});
});
Expand All @@ -46,7 +46,7 @@ UserSchema.pre('save', function (next) {
});

UserSchema.methods.comparePassword = function (passw, cb) {
bcrypt.compare(passw, this.password, function (err, isMatch) {
bcrypt.compare(passw, this.Password, function (err, isMatch) {
if (err) {
return cb(err);
}
Expand Down

0 comments on commit 1f3ad7c

Please sign in to comment.