diff --git a/config/database.js b/config/database.js index 22957ed..9201bf3 100644 --- a/config/database.js +++ b/config/database.js @@ -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 }; \ No newline at end of file diff --git a/controllers/UserController.js b/controllers/UserController.js index 05013e5..52708ca 100644 --- a/controllers/UserController.js +++ b/controllers/UserController.js @@ -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 @@ -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 { diff --git a/model/User.js b/model/User.js index 5711627..89d9432 100644 --- a/model/User.js +++ b/model/User.js @@ -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] }, @@ -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(); }); }); @@ -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); }