mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 08:42:12 +10:00
chore: implemented feedback + a small refactoring
This commit is contained in:
@ -3,3 +3,5 @@ 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;
|
||||
|
||||
@ -3,7 +3,7 @@ import crypto from 'crypto';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
// temporary choice for testing only
|
||||
import { ONE_WEEK } from '../../constants/time';
|
||||
import { ONE_YEAR } from '../../constants/time';
|
||||
|
||||
type CreateApiTokenInput = {
|
||||
userId: number;
|
||||
@ -22,7 +22,7 @@ export const createApiToken = async ({ userId, tokenName }: CreateApiTokenInput)
|
||||
token: tokenHash,
|
||||
name: tokenName,
|
||||
userId,
|
||||
expires: new Date(Date.now() + ONE_WEEK),
|
||||
expires: new Date(Date.now() + ONE_YEAR),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
export const checkUserFromToken = async ({ token }: { token: string }) => {
|
||||
const user = await prisma.user.findFirstOrThrow({
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
ApiToken: {
|
||||
some: {
|
||||
@ -9,7 +9,20 @@ export const checkUserFromToken = async ({ token }: { token: string }) => {
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
ApiToken: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new Error('Token not found');
|
||||
}
|
||||
|
||||
const tokenObject = user.ApiToken.find((apiToken) => apiToken.token === token);
|
||||
|
||||
if (!tokenObject || new Date(tokenObject.expires) < new Date()) {
|
||||
throw new Error('The API token has expired');
|
||||
}
|
||||
|
||||
return user;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user