refactor: Extract token generation logic to User model

This commit is contained in:
Faruk AYDIN
2024-03-26 13:14:10 +01:00
parent a2acdc6b12
commit 93b2098829

View File

@@ -5,6 +5,7 @@ import crypto from 'node:crypto';
import appConfig from '../config/app.js'; import appConfig from '../config/app.js';
import { hasValidLicense } from '../helpers/license.ee.js'; import { hasValidLicense } from '../helpers/license.ee.js';
import userAbility from '../helpers/user-ability.js'; import userAbility from '../helpers/user-ability.js';
import createAuthTokenByUserId from '../helpers/create-auth-token-by-user-id.js';
import Base from './base.js'; import Base from './base.js';
import Connection from './connection.js'; import Connection from './connection.js';
import Execution from './execution.js'; import Execution from './execution.js';
@@ -161,6 +162,17 @@ class User extends Base {
: Execution.query(); : Execution.query();
} }
static async authenticate(email, password) {
const user = await User.query().findOne({
email: email?.toLowerCase() || null,
});
if (user && (await user.login(password))) {
const token = createAuthTokenByUserId(user.id);
return token;
}
}
login(password) { login(password) {
return bcrypt.compare(password, this.password); return bcrypt.compare(password, this.password);
} }