seepur/app/Models/User.js

61 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-01-14 01:57:43 +00:00
'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 {
2020-02-02 22:42:18 +00:00
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 {
2020-03-17 22:16:34 +00:00
avatar: `https://api.adorable.io/avatars/285/${u.email}.png`, id: u.id,
name: u.name, isAdmin: u.is_admin
2020-02-02 22:42:18 +00:00
}
2020-01-14 01:57:43 +00:00
}
2020-03-17 22:16:34 +00:00
static get hidden() {
return ['password']
}
static get dates() {
return super.dates.concat(['last_logged_in'])
}
2020-01-14 01:57:43 +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}
*/
2020-02-02 22:42:18 +00:00
tokens() {
2020-01-14 01:57:43 +00:00
return this.hasMany('App/Models/Token')
}
links() {
return this.hasMany('App/Models/Link')
}
2020-01-14 01:57:43 +00:00
}
module.exports = User