mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
35 lines
787 B
TypeScript
35 lines
787 B
TypeScript
import crypto from 'crypto';
|
|
|
|
import { prisma } from '@documenso/prisma';
|
|
|
|
// temporary choice for testing only
|
|
import { ONE_WEEK } from '../../constants/time';
|
|
|
|
type CreateApiTokenInput = {
|
|
userId: number;
|
|
tokenName: string;
|
|
};
|
|
|
|
export const createApiToken = async ({ userId, tokenName }: CreateApiTokenInput) => {
|
|
// quick implementation for testing; it needs double checking
|
|
const tokenHash = crypto
|
|
.createHash('sha512')
|
|
.update(crypto.randomBytes(32).toString('hex'))
|
|
.digest('hex');
|
|
|
|
const token = await prisma.apiToken.create({
|
|
data: {
|
|
token: tokenHash,
|
|
name: tokenName,
|
|
userId,
|
|
expires: new Date(Date.now() + ONE_WEEK),
|
|
},
|
|
});
|
|
|
|
if (!token) {
|
|
throw new Error(`Failed to create the API token`);
|
|
}
|
|
|
|
return token;
|
|
};
|