mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
36 lines
790 B
TypeScript
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,
|
|
};
|
|
};
|