Guest User

Untitled

a guest
Sep 4th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. {
  2. "Roles": [
  3. "5b7fd72537651320a03494f8",
  4. "5b7fdc06e3fcb037d016e26a"
  5. ],
  6. "_id": "5b8e530be2d4630fd4f4b34a",
  7. "Username": "ctucker",
  8. "FirstName": "Chris",
  9. "LastName": "Tucker",
  10. "Email": "ctucker@innovate.com",
  11. "createdAt": "2018-09-04T09:40:27.091Z",
  12. "updatedAt": "2018-09-04T09:40:27.091Z",
  13. "__v": 0
  14. }
  15.  
  16. const userSchema = new mongoose.Schema({
  17.  
  18. Username: {
  19. type: String,
  20. required: true,
  21. minlength: 5,
  22. maxlength: 255,
  23. unique: true
  24. },
  25. FirstName: {
  26. type: String
  27. },
  28. LastName: {
  29. type: String
  30. },
  31. Email: {
  32. type: String,
  33. required: true,
  34. minlength: 5,
  35. maxlength: 255
  36. },
  37. Password: {
  38. type: String,
  39. required: true,
  40. minlength: 5,
  41. maxlength: 1024
  42. },
  43. Roles: [{type: mongoose.Schema.Types.ObjectId, ref: Roles}],
  44. Active: {type: Boolean, default: true},
  45. SuperUser: {type: Boolean, default: false}
  46. },{
  47. timestamps: true
  48. });
  49.  
  50. const rolesSchema = new mongoose.Schema({
  51. RoleName: {
  52. type: String,
  53. required: true,
  54. minlength: 5,
  55. maxlength: 255,
  56. unique: true
  57. },
  58. Description: {
  59. type: String
  60. },
  61. Active: {type: Boolean, default: true}
  62. }, {
  63. timestamps: true
  64. });
  65.  
  66. router.post('/', [auth, admin], async (req, res) => {
  67.  
  68. const { error } = validate(req.body);
  69. if (error) return res.status(400).send(error.details[0].message);
  70.  
  71. let user = await User.findOne({ Username: req.body.Username });
  72. if (user) return res.status(400).send('User with username: ', req.body.Username, 'already exists. Please try with any other username');
  73.  
  74. let roleInput = [];
  75.  
  76. if(req.body.Roles !== null) {
  77. req.body.Roles.forEach( async (element) => {
  78. objectId = mongoose.Types.ObjectId(element);
  79. roleInput.push(objectId);
  80. });
  81. }
  82.  
  83. console.log('Role info ', roleInput);
  84.  
  85. user = new User(_.pick(req.body, ['Username', 'FirstName', 'LastName' ,'Email', 'Password', 'Active','SuperUser']));
  86.  
  87. console.log('User Input => ', user);
  88.  
  89. const salt = await bcrypt.genSalt(10);
  90. user.Password = await bcrypt.hash(user.Password, salt);
  91.  
  92. roleInput.forEach( async (objectId) => {
  93. user.Roles.push(objectId);
  94. });
  95.  
  96. console.log('User Input after role addition => ', user);
  97.  
  98. await user.save();
  99.  
  100. const token = user.generateAuthToken();
  101. res.header('x-auth-token', token).send(_.pick(user, ['_id', 'FirstName', 'LastName' ,'Email', 'Roles']));
  102.  
  103. });
  104.  
  105. const user = await User.findOne().populate('Roles');
  106. // output => user.Roles // will be array of mongo documents
  107.  
  108. 'use strict';
  109.  
  110. let user = await User.findOne({
  111. Username: req.body.Username
  112. }).populate('Roles');
  113.  
  114. if (user && user.Roles.length > 0) {
  115. for (let role of user.Roles) {
  116. console.log(role.RoleName);// logs `RoleName`
  117. }
  118. }
Add Comment
Please, Sign In to add comment