Files
wr.do/lib/dto/api-key.ts
T
2024-10-29 18:23:14 +08:00

27 lines
589 B
TypeScript

import crypto from "crypto";
import { prisma } from "@/lib/db";
export const getApiKeyByUserId = async (userId: string) => {
return prisma.user.findUnique({
where: { id: userId },
select: { apiKey: true },
});
};
export const checkApiKey = async (apiKey: string) => {
return prisma.user.findFirst({
where: { apiKey },
select: { id: true },
});
};
export const generateApiKey = async (userId: string) => {
const apiKey = crypto.randomUUID();
return prisma.user.update({
where: { id: userId },
data: { apiKey },
select: { apiKey: true },
});
};