import type { ServiceMethodFactory } from "@ethicdevs/react-monolith";
import { User } from "@prisma/client";
import type { AuthServiceDeps } from "./types";
const makeIsUserLoginAllowed: ServiceMethodFactory<
AuthServiceDeps,
[string, string],
Promise<[boolean, User | null]>
> = ({ cryptoService, request }) => {
return async (emailAddress, password) => {
const passwordHash = cryptoService.computeHash(password);
const matchingUser = await request.prisma.user.findUnique({
where: {
email: emailAddress,
},
});
if (matchingUser == null) return [false, null];
const shouldAllowLogin = matchingUser.hashedPassword === passwordHash;
if (shouldAllowLogin === false) return [false, null];
return [true, matchingUser];
};
};
export default makeIsUserLoginAllowed;