feat: Implement token generation logic on api tokens
This commit is contained in:
@@ -1,17 +1,25 @@
|
||||
import Base from './base.js';
|
||||
|
||||
import crypto from 'crypto';
|
||||
class ApiToken extends Base {
|
||||
static tableName = 'api_tokens';
|
||||
|
||||
static jsonSchema = {
|
||||
type: 'object',
|
||||
required: ['token'],
|
||||
|
||||
properties: {
|
||||
id: { type: 'string', format: 'uuid' },
|
||||
token: { type: 'string', minLength: 32 },
|
||||
},
|
||||
};
|
||||
|
||||
async assignToken() {
|
||||
this.token = crypto.randomBytes(48).toString('hex');
|
||||
}
|
||||
|
||||
async $beforeInsert(queryContext) {
|
||||
await super.$beforeInsert(queryContext);
|
||||
await this.assignToken();
|
||||
}
|
||||
}
|
||||
|
||||
export default ApiToken;
|
||||
|
||||
Reference in New Issue
Block a user