Advertisement
Guest User

Login Data

a guest
Nov 9th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1.  
  2. var passwordHash = require('password-hash');
  3. var jwt = require('jsonwebtoken');
  4. module.exports = (app, db) => {
  5. /* GET home page. */
  6. app.post('/api/user/login', function (req, res) {
  7. console.log(req.body);
  8. var hashedPassword = passwordHash.generate(req.body.password);
  9. db.User.findAll({
  10. where: {
  11. email: req.body.email
  12. },
  13. include: [db.Company]
  14. }).then(function (users) {
  15. if (users.length > 0) {
  16. if (users[0].roleid == 2)
  17. {
  18. db.Enroller.findAll({
  19. where:{email:req.body.email}
  20. }).then(function(enrollers){
  21. if(enrollers.length > 0)
  22. {
  23. var passwordcheck = passwordHash.verify(req.body.password, users[0].password);
  24. if (passwordcheck) {
  25. // res.send({ id:users[0].id,username:users[0].username});
  26.  
  27. var token = jwt.sign({ user: users[0] }, "mysecret", {
  28. expiresIn: "24h" // expires in 24 hours
  29. });
  30. console.log(token);
  31. // return the information including token as JSON
  32. res.json({
  33. code: 200,
  34. success: true,
  35. message: 'Logged in successfully.',
  36. Data: users[0],
  37. LoginId:enrollers[0].id,
  38. email:enrollers[0].email,
  39. token: token
  40. });
  41. }
  42. else
  43. res.json({
  44. code: 300,
  45. success: true,
  46. message: 'Email/Password combination did not match. Please try again.',
  47. Data: ""
  48. });
  49.  
  50. }
  51. else{
  52. res.json({
  53. code: 300,
  54. success: true,
  55. message: 'User not found. Please re-enter correct credentials.',
  56. Data: ""
  57. });
  58.  
  59. }
  60. })
  61. }
  62. else
  63. {
  64. var passwordcheck = passwordHash.verify(req.body.password, users[0].password);
  65. if (passwordcheck) {
  66. var token = jwt.sign({ user: users[0] }, "mysecret", {
  67. expiresIn: "24h" // expires in 24 hours
  68. });
  69. res.json({
  70. code: 200,
  71. success: true,
  72. message: 'Logged in successfully.',
  73. Data: users[0],
  74. LoginId:users[0].id,
  75. email:users[0].email,
  76. token: token
  77. });
  78. }
  79. else {
  80. res.json({
  81. code: 300,
  82. success: true,
  83. message: 'Email/Password combination did not match. Please try again.',
  84. Data: ""
  85. });
  86.  
  87. }
  88.  
  89. }
  90. }
  91. else
  92. res.json({
  93. code: 300,
  94. success: true,
  95. message: 'Username not found!',
  96. Data: ""
  97. });
  98. });
  99. });
  100.  
  101. app.post('/api/user/registration', function (req, res) {
  102.  
  103. var hashedPassword = passwordHash.generate(req.body.password);
  104. db.User.findAll({
  105. where: {
  106. username: req.body.username
  107. }
  108. }).then(function (users) {
  109. if (users.length > 0) {
  110. res.json({
  111. code: 300,
  112. success: true,
  113. message: 'Already Registered!',
  114. Data: users
  115. });
  116. }
  117. else {
  118. db.Company.findAll(
  119. {
  120. where: { id: req.body.company_id }
  121. }
  122. ).then(function (company) {
  123. if (company.length > 0) {
  124. db.User.build({
  125. firstname: req.body.firstname,
  126. lastname: req.body.lastname,
  127. username: req.body.username,
  128. email: req.body.email,
  129. password: hashedPassword,
  130. last_login: req.body.last_login,
  131. roleid: req.body.roleid,
  132. status: req.body.status,
  133. company_id: company[0].id,
  134. Company: company[0]
  135. }, {
  136. include: [
  137. {
  138. model: db.Company,
  139. as: 'company'
  140. }
  141. ]
  142. }
  143. )
  144. .save()
  145. .then(function (data) {
  146. res.json({
  147. code: 200,
  148. success: true,
  149. message: 'Successfully registered!',
  150. Data: data
  151. });
  152. })
  153. .catch(error => {
  154. res.json({
  155. code: 500,
  156. success: false,
  157. message: 'Registration failed.',
  158. Data: error
  159. });
  160. // Ooops, do some error-handling
  161. });
  162.  
  163. }
  164. });
  165. }
  166.  
  167. });
  168. });
  169. }
  170.  
  171. // 200 - OK
  172. // 404 - Not Found
  173. // 500 - Internal Server Error
  174. // If you're not comfortable reducing all your error conditions to these 3, try picking among these additional 5:
  175.  
  176. // 201 - Created
  177. // 300- status message
  178. // 304 - Not Modified
  179. // 400 - Bad Request
  180. // 401 - Unauthorized
  181. // 403 - Forbidden
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement