PasswordAuthMethod: fix findUserByIdentifier()

This commit is contained in:
Alice Gaudon 2020-11-14 18:16:05 +01:00
parent b75b227ca1
commit f8c4906a51

View File

@ -12,7 +12,6 @@ import Controller from "../../Controller";
import UserPasswordComponent from "./UserPasswordComponent"; import UserPasswordComponent from "./UserPasswordComponent";
import UserNameComponent, {USERNAME_REGEXP} from "../models/UserNameComponent"; import UserNameComponent, {USERNAME_REGEXP} from "../models/UserNameComponent";
import ModelFactory from "../../db/ModelFactory"; import ModelFactory from "../../db/ModelFactory";
import {WhereOperator, WhereTest} from "../../db/ModelQuery";
import {ServerError} from "../../HttpError"; import {ServerError} from "../../HttpError";
export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof> { export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof> {
@ -30,12 +29,15 @@ export default class PasswordAuthMethod implements AuthMethod<PasswordAuthProof>
.with('user') .with('user')
.where('email', identifier); .where('email', identifier);
const user = (await query
.first())?.user.getOrFail();
if (user) return user;
if (ModelFactory.get(User).hasComponent(UserNameComponent)) { if (ModelFactory.get(User).hasComponent(UserNameComponent)) {
query.where('name', identifier, WhereTest.EQ, WhereOperator.OR); return await User.select().where('name', identifier).first();
} }
return (await query return null;
.first())?.user.getOrFail() || null;
} }
public async getProofsForSession(session: Express.Session): Promise<PasswordAuthProof[]> { public async getProofsForSession(session: Express.Session): Promise<PasswordAuthProof[]> {