adonis-fullstack-app/app/Models/User.js

37 lines
739 B
JavaScript
Raw Normal View History

2017-08-20 17:09:25 +00:00
'use strict'
const Hash = use('Hash')
2017-08-20 17:09:25 +00:00
const Model = use('Model')
class User extends Model {
static boot () {
super.boot()
/**
2018-02-04 09:50:08 +00:00
* A hook to hash the user password before saving
2017-08-20 17:09:25 +00:00
* it to the database.
*/
this.addHook('beforeCreate', async (userInstance) => {
if (userInstance.dirty.password) {
userInstance.password = await Hash.make(userInstance.password)
}
})
2017-08-20 17:09:25 +00:00
}
/**
* A relationship on tokens is required for auth to
* work. Since features like `refreshTokens` or
* `rememberToken` will be saved inside the
* tokens table.
*
* @method tokens
*
* @return {Object}
*/
tokens () {
return this.hasMany('App/Models/Token')
}
}
module.exports = User