mirror of
https://github.com/documenso/documenso.git
synced 2025-11-17 18:21:32 +10:00
feat: allow user to choose expiry date
This commit is contained in:
@ -1,7 +1,11 @@
|
||||
import { Duration } from 'luxon';
|
||||
|
||||
export const ONE_SECOND = 1000;
|
||||
export const ONE_MINUTE = ONE_SECOND * 60;
|
||||
export const ONE_HOUR = ONE_MINUTE * 60;
|
||||
export const ONE_DAY = ONE_HOUR * 24;
|
||||
export const ONE_WEEK = ONE_DAY * 7;
|
||||
export const ONE_MONTH = ONE_DAY * 30;
|
||||
export const ONE_YEAR = ONE_DAY * 365;
|
||||
export const ONE_MONTH = Duration.fromObject({ months: 1 });
|
||||
export const THREE_MONTHS = Duration.fromObject({ months: 3 });
|
||||
export const SIX_MONTHS = Duration.fromObject({ months: 6 });
|
||||
export const ONE_YEAR = Duration.fromObject({ years: 1 });
|
||||
|
||||
@ -1,26 +1,42 @@
|
||||
import type { Duration } from 'luxon';
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
// temporary choice for testing only
|
||||
import { ONE_YEAR } from '../../constants/time';
|
||||
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 }: CreateApiTokenInput) => {
|
||||
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: new Date(Date.now() + ONE_YEAR),
|
||||
expires: expirationDate
|
||||
? DateTime.now().plus(timeConstantsRecords[expirationDate]).toJSDate()
|
||||
: null,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "ApiToken" ALTER COLUMN "expires" DROP NOT NULL;
|
||||
@ -103,7 +103,7 @@ model ApiToken {
|
||||
name String
|
||||
token String @unique
|
||||
algorithm ApiTokenAlgorithm @default(SHA512)
|
||||
expires DateTime
|
||||
expires DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
userId Int
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@ -46,10 +46,12 @@ export const apiTokenRouter = router({
|
||||
.input(ZCreateTokenMutationSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
try {
|
||||
const { tokenName } = input;
|
||||
const { tokenName, expirationDate } = input;
|
||||
|
||||
return await createApiToken({
|
||||
userId: ctx.user.id,
|
||||
tokenName,
|
||||
expirationDate,
|
||||
});
|
||||
} catch (e) {
|
||||
throw new TRPCError({
|
||||
|
||||
@ -8,6 +8,7 @@ export type TGetApiTokenByIdQuerySchema = z.infer<typeof ZGetApiTokenByIdQuerySc
|
||||
|
||||
export const ZCreateTokenMutationSchema = z.object({
|
||||
tokenName: z.string().min(3, { message: 'The token name should be 3 characters or longer' }),
|
||||
expirationDate: z.string().nullable(),
|
||||
});
|
||||
|
||||
export type TCreateTokenMutationSchema = z.infer<typeof ZCreateTokenMutationSchema>;
|
||||
|
||||
Reference in New Issue
Block a user