You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lib/src/auth/orm/ORMUserRepository.ts

42 lines
1.5 KiB

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<Maybe<Authenticatable>> {
return ORMUser.query<ORMUser>()
.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<string, string>): Promise<Maybe<Authenticatable>> {
if ( credentials.securityIdentifier ) {
return ORMUser.query<ORMUser>()
.where('username', '=', credentials.securityIdentifier)
.first()
}
if ( credentials.username && credentials.password ) {
const user = await ORMUser.query<ORMUser>()
.where('username', '=', credentials.username)
.first()
if ( user && await user.verifyPassword(credentials.password) ) {
return user
}
}
}
}