mirror of
https://github.com/linkwarden/linkwarden.git
synced 2026-06-30 15:56:58 +00:00
61 lines
1.2 KiB
TypeScript
61 lines
1.2 KiB
TypeScript
import { prisma } from "@linkwarden/prisma";
|
|
import { User } from "@linkwarden/prisma/client";
|
|
import bcrypt from "bcrypt";
|
|
|
|
type Props = {
|
|
username: string;
|
|
password: string;
|
|
};
|
|
|
|
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY;
|
|
const emailEnabled =
|
|
process.env.EMAIL_FROM && process.env.EMAIL_SERVER ? true : false;
|
|
|
|
export default async function verifyByCredentials({
|
|
username,
|
|
password,
|
|
}: Props): Promise<User | null> {
|
|
const user = await prisma.user.findFirst({
|
|
where: emailEnabled
|
|
? {
|
|
OR: [
|
|
{
|
|
username: username.toLowerCase(),
|
|
},
|
|
{
|
|
email: username?.toLowerCase(),
|
|
},
|
|
],
|
|
}
|
|
: {
|
|
username: username.toLowerCase(),
|
|
},
|
|
include: {
|
|
subscriptions: true,
|
|
parentSubscription: true,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
return null;
|
|
}
|
|
|
|
if (emailEnabled && !user.emailVerified) {
|
|
return null;
|
|
}
|
|
|
|
let passwordMatches: boolean = false;
|
|
|
|
if (user?.password) {
|
|
passwordMatches = bcrypt.compareSync(password, user.password);
|
|
|
|
if (!passwordMatches) {
|
|
return null;
|
|
} else {
|
|
return user;
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|