mirror of
https://github.com/documenso/documenso.git
synced 2025-11-21 20:21:38 +10:00
wip
This commit is contained in:
30
packages/auth/server/lib/session.ts
Normal file
30
packages/auth/server/lib/session.ts
Normal 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,
|
||||
};
|
||||
};
|
||||
5
packages/auth/server/lib/tokens.ts
Normal file
5
packages/auth/server/lib/tokens.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { customAlphabet } from 'nanoid';
|
||||
|
||||
const sessionTokenId = customAlphabet('abcdefhiklmnorstuvwxz', 10);
|
||||
|
||||
export const createSessionToken = (length = 10) => `session_${sessionTokenId(length)}` as const;
|
||||
Reference in New Issue
Block a user