.ts
TypeScript
(application/typescript)
// 1st-party
import type { ServiceMethodFactory } from "@ethicdevs/react-monolith";
// generated via script[generate:prisma]
import { User } from "@prisma/client";
// app
import type { AuthServiceDeps } from "./types";

const makeShouldAllowUserLogin: 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 makeShouldAllowUserLogin;