User.js 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict'
  2. /** @type {import('@adonisjs/framework/src/Hash')} */
  3. const Hash = use('Hash')
  4. /** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
  5. const Model = use('Model')
  6. class User extends Model {
  7. static finduser (type, value) {
  8. return this.findBy(type, value)
  9. }
  10. static boot () {
  11. super.boot()
  12. /**
  13. * A hook to hash the user password before saving
  14. * it to the database.
  15. */
  16. this.addHook('beforeSave', async (userInstance) => {
  17. if (userInstance.dirty.password) {
  18. userInstance.password = await Hash.make(userInstance.password)
  19. }
  20. })
  21. }
  22. /**
  23. * A relationship on tokens is required for auth to
  24. * work. Since features like `refreshTokens` or
  25. * `rememberToken` will be saved inside the
  26. * tokens table.
  27. *
  28. * @method tokens
  29. *
  30. * @return {Object}
  31. */
  32. tokens () {
  33. return this.hasMany('App/Models/Token')
  34. }
  35. }
  36. module.exports = User