user.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. let bcrypt = require('bcryptjs')
  2. let randomColor = require('randomcolor')
  3. const Errors = require('../lib/errors.js')
  4. module.exports = (sequelize, DataTypes) => {
  5. let User = sequelize.define('User', {
  6. username: {
  7. type: DataTypes.STRING,
  8. unique: true,
  9. validate: {
  10. len: {
  11. args: [6, 50],
  12. msg: 'username must be between 6 and 50 characters'
  13. },
  14. isString (val) {
  15. if(typeof val !== 'string') {
  16. throw new sequelize.ValidationError('username must be a string')
  17. }
  18. }
  19. }
  20. },
  21. description: {
  22. type: DataTypes.TEXT,
  23. validate: {
  24. isString (val) {
  25. if(typeof val !== 'string') {
  26. throw new sequelize.ValidationError('description must be a string')
  27. }
  28. },
  29. len: {
  30. args: [0, 1024],
  31. msg: 'description must be less than 1024 characters'
  32. }
  33. }
  34. },
  35. color: {
  36. type: DataTypes.STRING,
  37. defaultValue () {
  38. return randomColor()
  39. }
  40. },
  41. hash: {
  42. type: DataTypes.STRING,
  43. allowNull: false,
  44. validate: {
  45. len: {
  46. args: [6, 100],
  47. msg: 'password must be between 6 and 100 characters'
  48. },
  49. isString (val) {
  50. if(typeof val !== 'string') {
  51. throw new sequelize.ValidationError('password must be a string')
  52. }
  53. }
  54. }
  55. },
  56. admin: {
  57. type: DataTypes.BOOLEAN,
  58. defaultValue: false
  59. }
  60. }, {
  61. instanceMethods: {
  62. async updatePassword (currentPassword, newPassword) {
  63. if(currentPassword === newPassword) {
  64. throw Errors.passwordSame
  65. } else if(typeof currentPassword !== 'string' || typeof newPassword !== 'string') {
  66. throw new sequelize.ValidationError('password must be a string')
  67. }
  68. let correctPassword = await bcrypt.compare(currentPassword, this.hash)
  69. if(correctPassword) {
  70. await this.update({ hash: newPassword })
  71. } else {
  72. throw Errors.invalidLoginCredentials
  73. }
  74. },
  75. async comparePassword (password) {
  76. return await bcrypt.compare(password, this.hash)
  77. }
  78. },
  79. classMethods: {
  80. associate (models) {
  81. User.hasMany(models.Post)
  82. User.hasMany(models.Thread)
  83. },
  84. includeOptions (from, limit) {
  85. let models = sequelize.models
  86. let options = models.Post.includeOptions()
  87. return [{
  88. model: models.Post,
  89. include: options,
  90. limit,
  91. where: { postNumber: { $gte: from } },
  92. order: [['id', 'ASC']]
  93. }]
  94. },
  95. async canBeAdmin (token) {
  96. let { User, AdminToken } = sequelize.models
  97. let adminUser = await User.findOne({ where: {
  98. admin: true
  99. }})
  100. if(adminUser) {
  101. if(token) {
  102. let adminToken = await AdminToken.findOne({ where: { token } })
  103. if(adminToken && adminToken.isValid()) {
  104. await adminToken.destroy()
  105. return true
  106. } else {
  107. throw Errors.invalidToken
  108. }
  109. } else {
  110. throw Errors.missingParameter('token')
  111. }
  112. } else {
  113. return true
  114. }
  115. }
  116. },
  117. hooks: {
  118. async afterValidate(user, options) {
  119. if(user.changed('hash') && user.hash.length <= 50) {
  120. user.hash = await bcrypt.hash(user.hash, 12)
  121. }
  122. options.hooks = false
  123. return options
  124. }
  125. }
  126. })
  127. return User
  128. }