user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.post('/login', async (req, res) => {
  59. let user, bcryptRes, validationErrors = []
  60. try {
  61. //Validations
  62. if(req.body.username === undefined) {
  63. validationErrors.push(Errors.missingParameter('username'))
  64. } else if(typeof req.body.username !== 'string') {
  65. validationErrors.push(Errors.invalidParameterType('username', 'string'))
  66. }
  67. if(req.body.password === undefined) {
  68. validationErrors.push(Errors.missingParameter('password'))
  69. } else if(typeof req.body.password !== 'string') {
  70. validationErrors.push(Errors.invalidParameterType('password', 'string'))
  71. }
  72. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  73. user = await User.findOne({
  74. where: {
  75. username: req.body.username,
  76. }
  77. })
  78. if(user) {
  79. bcryptRes = await bcrypt.compare(req.body.password, user.hash)
  80. if(bcryptRes) {
  81. req.session.loggedIn = true;
  82. res.json({
  83. username: user.username,
  84. success: true
  85. })
  86. } else {
  87. res.status(401)
  88. res.json({
  89. errors: [Errors.invalidLoginCredentials]
  90. })
  91. }
  92. } else {
  93. res.status(401)
  94. res.json({
  95. errors: [Errors.invalidLoginCredentials]
  96. })
  97. }
  98. } catch (err) {
  99. if(err === Errors.VALIDATION_ERROR) {
  100. res.status(400)
  101. res.json({
  102. errors: validationErrors
  103. })
  104. } else {
  105. console.log(err)
  106. res.status(500)
  107. res.json({
  108. errors: [Errors.unknown]
  109. })
  110. }
  111. }
  112. })
  113. router.post('/logout', async (req, res) => {
  114. req.session.loggedIn = false;
  115. res.json({
  116. success: true
  117. })
  118. })
  119. module.exports = router