mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 12:32:34 +10:00
52 lines
1.1 KiB
TypeScript
52 lines
1.1 KiB
TypeScript
import type { Duration } from 'luxon';
|
|
import { DateTime } from 'luxon';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
// temporary choice for testing only
|
|
import * as timeConstants from '../../constants/time';
|
|
import { alphaid } from '../../universal/id';
|
|
import { hashString } from '../auth/hash';
|
|
|
|
type TimeConstants = typeof timeConstants & {
|
|
[key: string]: number | Duration;
|
|
};
|
|
|
|
type CreateApiTokenInput = {
|
|
userId: number;
|
|
tokenName: string;
|
|
expirationDate: string | null;
|
|
};
|
|
|
|
export const createApiToken = async ({
|
|
userId,
|
|
tokenName,
|
|
expirationDate,
|
|
}: CreateApiTokenInput) => {
|
|
const apiToken = `api_${alphaid(16)}`;
|
|
|
|
const hashedToken = hashString(apiToken);
|
|
|
|
const timeConstantsRecords: TimeConstants = timeConstants;
|
|
|
|
const dbToken = await prisma.apiToken.create({
|
|
data: {
|
|
token: hashedToken,
|
|
name: tokenName,
|
|
userId,
|
|
expires: expirationDate
|
|
? DateTime.now().plus(timeConstantsRecords[expirationDate]).toJSDate()
|
|
: null,
|
|
},
|
|
});
|
|
|
|
if (!dbToken) {
|
|
throw new Error('Failed to create the API token');
|
|
}
|
|
|
|
return {
|
|
id: dbToken.id,
|
|
token: apiToken,
|
|
};
|
|
};
|