Skip to content

Commit

Permalink
added service to create user, and account type for validating multipl…
Browse files Browse the repository at this point in the history
…e user
  • Loading branch information
robophil committed Feb 5, 2017
1 parent 4b59463 commit 93983b1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 37 deletions.
51 changes: 18 additions & 33 deletions api/controllers/JwtController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var bcrypt = require('bcryptjs');

module.exports = {
login: function (req, res) {
auth: function (req, res) {
// Validate request paramaters
if (!req.body.email || !req.body.password) {
return res.json(401, {
Expand Down Expand Up @@ -48,41 +48,26 @@ module.exports = {
},

signup: function (req, res) {
// Validate request paramaters
if (!req.body.email || !req.body.password || !req.body.confirmPassword) {
return res.json(400, {
err: {
status: 'danger',
message: 'email, password or confirmPassword parameter(s) missing'
}
});
}

//TODO: Do some validation on the input
if (req.body.password !== req.body.confirmPassword) {
return res.json(400, {
err: {
status: 'danger',
message: 'passwords do not match'
}
});
}

//new user object
var newUser = {
email: req.body.email,
password: req.body.password,
active: sails.config.jsonWebToken.default_account_status
};

User.create(newUser).then((user) => {
JwtService.createUser(req).then( user => {
JwtService.issueToken({ user_id: user.id }, user).then((token) => {
return res.json({ user: user, token: token });
}).catch((err) => {
return res.serverError(err);
return res.json(401, {
err: {
status: 'danger',
message: 'Unable to create token for new account !!, account created'
},
message: err
});
});
}).catch((err) => {
return res.serverError(err);
});
}).catch( err => {
return res.json(400, {
err: {
status: 'danger',
message: 'Unable to create account'
},
message: err
});
})
}
};
20 changes: 20 additions & 0 deletions api/models/AccountType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* AccountType.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/documentation/concepts/models-and-orm/models
*/

module.exports = {

attributes: {
type: {
type: 'string',
required: 'true'
},
owner: {
model: 'user'
}
}
};

7 changes: 6 additions & 1 deletion api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ module.exports = {
password: {
type: 'string',
required: true,
minLength: 6
minLength: 4
},

accountType: {
collection: 'accounttype',
via: 'owner'
},

active: {
Expand Down
27 changes: 26 additions & 1 deletion api/services/JwtService.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,29 @@ module.exports.verifyToken = function (token) {
resolve(decoded);
});
});
};
};

module.exports.createUser = function (req){
return new Promise ((resolve, reject) => {
// Validate request paramaters
if (!req.body.email || !req.body.password) {
reject('email or password parameter(s) missing')
}

//new user object
var newUser = {
email: req.body.email,
password: req.body.password,
active: sails.config.jsonWebToken.default_account_status,
accountType: {
type : req.body.type || 'user'
}
};

User.create(newUser).then((user) => {
resolve(user)
}).catch((err) => {
reject(err)
});
});
}
2 changes: 1 addition & 1 deletion config/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports.routes = {
'POST /jwt/auth': 'JwtController.login',
'POST /jwt/auth': 'JwtController.auth',
'POST /jwt/signup': 'JwtController.signup'
};
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"bcryptjs": "^2.4.0",
"jsonwebtoken": "^7.1.9",
"lodash": "^4.16.4",
"nodemailer": "^2.6.4",
"sails-build-dictionary": "^0.10.1",
"sails-util": "^0.11.0"
}
Expand Down

0 comments on commit 93983b1

Please sign in to comment.