import {Authenticatable, AuthenticatableIdentifier, AuthenticatableRepository} from '../types' import {Awaitable, Maybe} from '../../util' import {ORMUser} from './ORMUser' import {Injectable} from '../../di' /** * A user repository implementation that looks up users stored in the database. */ @Injectable() export class ORMUserRepository extends AuthenticatableRepository { /** Look up the user by their username. */ getByIdentifier(id: AuthenticatableIdentifier): Awaitable> { return ORMUser.query() .where('username', '=', id) .first() } /** * Try to look up a user by the credentials provided. * If a securityIdentifier is specified, look up the user by username. * If username/password are specified, look up the user and verify the password. * @param credentials */ async getByCredentials(credentials: Record): Promise> { if ( credentials.securityIdentifier ) { return ORMUser.query() .where('username', '=', credentials.securityIdentifier) .first() } if ( credentials.username && credentials.password ) { const user = await ORMUser.query() .where('username', '=', credentials.username) .first() if ( user && await user.verifyPassword(credentials.password) ) { return user } } } }