Files
documenso/packages/lib/server-only/public-api/create-api-token.ts
2024-01-17 12:44:25 +02:00

36 lines
790 B
TypeScript

import { prisma } from '@documenso/prisma';
// temporary choice for testing only
import { ONE_YEAR } from '../../constants/time';
import { alphaid } from '../../universal/id';
import { hashString } from '../auth/hash';
type CreateApiTokenInput = {
userId: number;
tokenName: string;
};
export const createApiToken = async ({ userId, tokenName }: CreateApiTokenInput) => {
const apiToken = `api_${alphaid(16)}`;
const hashedToken = hashString(apiToken);
const dbToken = await prisma.apiToken.create({
data: {
token: hashedToken,
name: tokenName,
userId,
expires: new Date(Date.now() + ONE_YEAR),
},
});
if (!dbToken) {
throw new Error('Failed to create the API token');
}
return {
id: dbToken.id,
token: apiToken,
};
};