forked from sagi/seepur
55 lines
1.1 KiB
JavaScript
55 lines
1.1 KiB
JavaScript
'use strict'
|
|
|
|
/** @type {import('@adonisjs/framework/src/Hash')} */
|
|
const Hash = use('Hash')
|
|
|
|
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
|
|
const Model = use('Model')
|
|
|
|
class User extends Model {
|
|
static boot() {
|
|
super
|
|
.boot()
|
|
|
|
/**
|
|
* A hook to hash the user password before saving
|
|
* it to the database.
|
|
*/
|
|
this.addHook('beforeSave', async (userInstance) => {
|
|
if (userInstance.dirty.password) {
|
|
userInstance.password = await Hash.make(userInstance.password)
|
|
}
|
|
})
|
|
}
|
|
|
|
publicJSON() {
|
|
const u = this.toJSON();
|
|
return {
|
|
avatar: u.avatar, email: u.email, name: u.name, isAdmin: false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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')
|
|
}
|
|
|
|
children() {
|
|
return this.hasMany('App/Models/Child')
|
|
}
|
|
|
|
links() {
|
|
return this.hasMany('App/Models/Link')
|
|
}
|
|
}
|
|
|
|
module.exports = User
|