Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

1.3 KB
Raw Blame
import type { z } from "zod/v4";

import { createLogger } from "@homarr/core/infrastructure/logs";
import type { Database } from "@homarr/db";
import { and, eq } from "@homarr/db";
import { users } from "@homarr/db/schema";
import type { userSignInSchema } from "@homarr/validation/user";

import { comparePasswordsAsync } from "../../../security";

const logger = createLogger({ module: "basicAuthorization" });

export const authorizeWithBasicCredentialsAsync = async (
  db: Database,
  credentials: z.infer<typeof userSignInSchema>,
  ipAddress: string,
) => {
  const user = await db.query.users.findFirst({
    where: and(eq(users.name, credentials.name.toLowerCase()), eq(users.provider, "credentials")),
  });

  if (!user?.password) {
    logger.warn("User not found", { userName: credentials.name, ipAddress });
    return null;
  }

  logger.info("User is trying to log in. Checking password...", { userName: user.name });
  const isValidPassword = await comparePasswordsAsync(credentials.password, user.password);

  if (!isValidPassword) {
    logger.warn("Password for user was incorrect", { userName: user.name, ipAddress });
    return null;
  }

  logger.info("User successfully authorized", { userName: user.name });

  return {
    id: user.id,
    name: user.name,
  };
};