user.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. req.session.loggedIn = true
  39. req.session.username = user.username
  40. res.json(user.toJSON())
  41. } catch (err) {
  42. if(err === Errors.VALIDATION_ERROR) {
  43. res.status(400)
  44. res.json({
  45. errors: validationErrors
  46. })
  47. } else if(err.name === 'SequelizeUniqueConstraintError') {
  48. res.status(400)
  49. res.json({
  50. errors: [Errors.accountAlreadyCreated]
  51. })
  52. } else {
  53. res.status(500)
  54. res.json({
  55. errors: [Errors.unknown]
  56. })
  57. }
  58. }
  59. })
  60. router.get('/:username', async (req, res) => {
  61. try {
  62. if(
  63. !req.session.loggedIn ||
  64. req.session.username !== req.params.username
  65. ) {
  66. throw Errors.requestNotAuthorized
  67. }
  68. let user = await User.findOne({
  69. attributes: { exclude: ['hash', 'id'] },
  70. where: { username: req.params.username }
  71. })
  72. res.json(user.toJSON())
  73. } catch (err) {
  74. if(err === Errors.requestNotAuthorized) {
  75. res.status(403)
  76. res.json({
  77. errors: [Errors.requestNotAuthorized]
  78. })
  79. } else {
  80. console.log(err)
  81. res.status(500)
  82. res.json({
  83. errors: [Errors.unknown]
  84. })
  85. }
  86. }
  87. })
  88. router.post('/:username/login', async (req, res) => {
  89. let user, bcryptRes, validationErrors = []
  90. try {
  91. //Validations
  92. if(req.body.password === undefined) {
  93. validationErrors.push(Errors.missingParameter('password'))
  94. } else if(typeof req.body.password !== 'string') {
  95. validationErrors.push(Errors.invalidParameterType('password', 'string'))
  96. }
  97. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  98. user = await User.findOne({
  99. where: {
  100. username: req.params.username,
  101. }
  102. })
  103. if(user) {
  104. bcryptRes = await bcrypt.compare(req.body.password, user.hash)
  105. if(bcryptRes) {
  106. req.session.loggedIn = true
  107. req.session.username = user.username
  108. res.json({
  109. username: user.username,
  110. success: true
  111. })
  112. } else {
  113. res.status(401)
  114. res.json({
  115. errors: [Errors.invalidLoginCredentials]
  116. })
  117. }
  118. } else {
  119. res.status(401)
  120. res.json({
  121. errors: [Errors.invalidLoginCredentials]
  122. })
  123. }
  124. } catch (err) {
  125. if(err === Errors.VALIDATION_ERROR) {
  126. res.status(400)
  127. res.json({
  128. errors: validationErrors
  129. })
  130. } else {
  131. console.log(err)
  132. res.status(500)
  133. res.json({
  134. errors: [Errors.unknown]
  135. })
  136. }
  137. }
  138. })
  139. router.post('/:username/logout', async (req, res) => {
  140. req.session.loggedIn = false
  141. req.session.username = undefined
  142. res.json({
  143. success: true
  144. })
  145. })
  146. module.exports = router