user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. let bcrypt = require('bcryptjs')
  2. let express = require('express')
  3. let router = express.Router()
  4. const Errors = require('../lib/errors.js')
  5. let User = require('../models').User
  6. router.post('/', async (req, res) => {
  7. let user, hash, validationErrors = [];
  8. try {
  9. //Validations
  10. if(req.body.username === undefined) {
  11. validationErrors.push(Errors.missingParameter('username'))
  12. } else {
  13. if(typeof req.body.username !== 'string') {
  14. validationErrors.push(Errors.invalidParameterType('username', 'string'))
  15. } if(req.body.username.length < 6) {
  16. validationErrors.push(Errors.parameterLengthTooSmall('username', 6))
  17. } if(req.body.username.length > 50) {
  18. validationErrors.push(Errors.parameterLengthTooLarge('username', 50))
  19. }
  20. }
  21. if(req.body.password === undefined) {
  22. validationErrors.push(Errors.missingParameter('password'))
  23. } else {
  24. if(typeof req.body.password !== 'string') {
  25. validationErrors.push(Errors.invalidParameterType('password', 'string'))
  26. } if(req.body.password.length < 6) {
  27. validationErrors.push(Errors.parameterLengthTooSmall('password', 6))
  28. } if(req.body.password.length > 100) {
  29. validationErrors.push(Errors.parameterLengthTooLarge('password', 100))
  30. }
  31. }
  32. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  33. hash = await bcrypt.hash(req.body.password, 12)
  34. user = await User.create({
  35. username: req.body.username,
  36. hash: hash
  37. })
  38. res.json(user.toJSON())
  39. } catch (err) {
  40. if(err === Errors.VALIDATION_ERROR) {
  41. res.status(400)
  42. res.json({
  43. errors: validationErrors
  44. })
  45. } else if(err.name === 'SequelizeUniqueConstraintError') {
  46. res.status(400)
  47. res.json({
  48. errors: [Errors.accountAlreadyCreated]
  49. })
  50. } else {
  51. res.status(500)
  52. res.json({
  53. errors: [Errors.unknown]
  54. })
  55. }
  56. }
  57. })
  58. router.get('/:username', async (req, res) => {
  59. try {
  60. if(
  61. !req.session.loggedIn ||
  62. req.session.username !== req.params.username
  63. ) {
  64. throw Errors.requestNotAuthorized
  65. }
  66. let user = async User.findOne({
  67. attributes: { exclude: ['hash', 'id'] },
  68. where: { username: req.params.username }
  69. })
  70. res.json(user.toJSON())
  71. } catch (err) {
  72. if(err === Errors.requestNotAuthorized) {
  73. res.json({
  74. errors: [Errors.requestNotAuthorized]
  75. })
  76. } else {
  77. console.log(err)
  78. res.status(500)
  79. res.json({
  80. errors: [Errors.unknown]
  81. })
  82. }
  83. }
  84. })
  85. router.post('/:username/login', async (req, res) => {
  86. let user, bcryptRes, validationErrors = []
  87. try {
  88. //Validations
  89. if(req.body.password === undefined) {
  90. validationErrors.push(Errors.missingParameter('password'))
  91. } else if(typeof req.body.password !== 'string') {
  92. validationErrors.push(Errors.invalidParameterType('password', 'string'))
  93. }
  94. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  95. user = await User.findOne({
  96. where: {
  97. username: req.params.username,
  98. }
  99. })
  100. if(user) {
  101. bcryptRes = await bcrypt.compare(req.body.password, user.hash)
  102. if(bcryptRes) {
  103. req.session.loggedIn = true
  104. req.session.username = user.username
  105. res.json({
  106. username: user.username,
  107. success: true
  108. })
  109. } else {
  110. res.status(401)
  111. res.json({
  112. errors: [Errors.invalidLoginCredentials]
  113. })
  114. }
  115. } else {
  116. res.status(401)
  117. res.json({
  118. errors: [Errors.invalidLoginCredentials]
  119. })
  120. }
  121. } catch (err) {
  122. if(err === Errors.VALIDATION_ERROR) {
  123. res.status(400)
  124. res.json({
  125. errors: validationErrors
  126. })
  127. } else {
  128. console.log(err)
  129. res.status(500)
  130. res.json({
  131. errors: [Errors.unknown]
  132. })
  133. }
  134. }
  135. })
  136. router.post('/:username/logout', async (req, res) => {
  137. req.session.loggedIn = false
  138. req.session.username = undefined
  139. res.json({
  140. success: true
  141. })
  142. })
  143. module.exports = router