32 lines
712 B
JavaScript
32 lines
712 B
JavaScript
const { Model } = require('flitter-orm')
|
|
const uuid = require('uuid').v4
|
|
|
|
class User extends Model {
|
|
static get services() {
|
|
return [...super.services, 'models']
|
|
}
|
|
|
|
static get schema() {
|
|
return {
|
|
uuid: { type: String, default: uuid },
|
|
username: String,
|
|
}
|
|
}
|
|
|
|
async get_token() {
|
|
const Token = this.models.get('Token')
|
|
const existing = await Token.findOne({
|
|
active: true,
|
|
user_uuid: this.uuid,
|
|
})
|
|
|
|
if ( existing ) return existing
|
|
|
|
const generated = new Token({ user_uuid: this.uuid })
|
|
await generated.save()
|
|
return generated
|
|
}
|
|
}
|
|
|
|
module.exports = exports = User
|