Files
Homarr/packages/auth/providers/credentials/authorization/basic-authorization.ts
Meier Lukas 75ba3f2ae7 refactor: remove central validation export to improve typescript performance (#2810)
* refactor: remove central validation export to improve typescript performance

* fix: missing package exports change in validation package

* chore: address pull request feedback
2025-04-06 10:37:28 +00:00

38 lines
1.0 KiB
TypeScript

import bcrypt from "bcrypt";
import type { z } from "zod";
import type { Database } from "@homarr/db";
import { and, eq } from "@homarr/db";
import { users } from "@homarr/db/schema";
import { logger } from "@homarr/log";
import type { userSignInSchema } from "@homarr/validation/user";
export const authorizeWithBasicCredentialsAsync = async (
db: Database,
credentials: z.infer<typeof userSignInSchema>,
) => {
const user = await db.query.users.findFirst({
where: and(eq(users.name, credentials.name.toLowerCase()), eq(users.provider, "credentials")),
});
if (!user?.password) {
logger.info(`user ${credentials.name} was not found`);
return null;
}
logger.info(`user ${user.name} is trying to log in. checking password...`);
const isValidPassword = await bcrypt.compare(credentials.password, user.password);
if (!isValidPassword) {
logger.warn(`password for user ${user.name} was incorrect`);
return null;
}
logger.info(`user ${user.name} successfully authorized`);
return {
id: user.id,
name: user.name,
};
};