This commit is contained in:
Mythie
2025-01-06 14:44:20 +11:00
parent 866b036484
commit 7009995204
12 changed files with 235 additions and 0 deletions

View File

@ -0,0 +1,30 @@
import { prisma } from '@documenso/prisma';
import { AuthenticationErrorCode } from '../error-codes';
import { AuthenticationError } from '../errors';
export const getSession = async (token: string) => {
const result = await prisma.session.findUnique({
where: {
sessionToken: token,
},
include: {
user: true,
},
});
if (!result) {
throw new AuthenticationError(AuthenticationErrorCode.SessionNotFound);
}
if (result.expires < new Date()) {
throw new AuthenticationError(AuthenticationErrorCode.SessionExpired);
}
const { user, ...session } = result;
return {
session,
user,
};
};

View File

@ -0,0 +1,5 @@
import { customAlphabet } from 'nanoid';
const sessionTokenId = customAlphabet('abcdefhiklmnorstuvwxz', 10);
export const createSessionToken = (length = 10) => `session_${sessionTokenId(length)}` as const;