diff --git a/src/server/lib/passport.ts b/src/server/lib/passport.ts index 067871816..c62e2fa31 100644 --- a/src/server/lib/passport.ts +++ b/src/server/lib/passport.ts @@ -9,16 +9,30 @@ import { import { Strategy as LocalStrategy } from 'passport-local'; import { prisma } from './prisma'; -export const generateToken = (userId: string) => { - return jwt.sign({ id: userId }, String(process.env.TOKEN_SECRET), { - expiresIn: String(process.env.TOKEN_EXPIRATION || '15m'), - }); +export const generateToken = ( + id: string, + otherProperties?: { [key: string]: any } +) => { + return jwt.sign( + { id, ...otherProperties }, + String(process.env.TOKEN_SECRET), + { + expiresIn: String(process.env.TOKEN_EXPIRATION || '15m'), + } + ); }; -export const generateRefreshToken = (userId: string) => { - return jwt.sign({ id: userId }, String(process.env.TOKEN_SECRET), { - expiresIn: String(process.env.TOKEN_REFRESH_EXPIRATION || '90d'), - }); +export const generateRefreshToken = ( + id: string, + otherProperties?: { [key: string]: any } +) => { + return jwt.sign( + { id, ...otherProperties }, + String(process.env.TOKEN_SECRET), + { + expiresIn: String(process.env.TOKEN_REFRESH_EXPIRATION || '90d'), + } + ); }; const authenticateUser = async ( @@ -55,7 +69,6 @@ passport.use( await prisma.user .findUnique({ include: { - serverCredentials: true, serverFolderPermissions: true, serverPermissions: true, }, diff --git a/src/server/lib/prisma.ts b/src/server/lib/prisma.ts index 9338a55b1..ffaacad30 100644 --- a/src/server/lib/prisma.ts +++ b/src/server/lib/prisma.ts @@ -1,4 +1,4 @@ -import { PrismaClient } from '@prisma/client'; +import { Prisma, PrismaClient } from '@prisma/client'; export const prisma = new PrismaClient({ errorFormat: 'minimal' }); export const exclude = ( @@ -17,7 +17,7 @@ function sleep(ms: number) { } prisma.$use(async (params, next) => { - const maxRetries = 5; + const maxRetries = 3; let retries = 0; do { @@ -25,8 +25,29 @@ prisma.$use(async (params, next) => { const result = await next(params); return result; } catch (err) { + console.log('err', err); + if (err instanceof Prisma.PrismaClientKnownRequestError) { + if (err.code === 'P2002') { + retries = 3; // Don't retry on unique constraint violation + return null; + } + } retries += 1; - return sleep(500); + return sleep(100); } } while (retries < maxRetries); }); + +// prisma.$use(async (params, next) => { +// const before = Date.now(); + +// const result = await next(params); + +// const after = Date.now(); + +// console.log( +// `Query ${params.model}.${params.action} took ${after - before}ms` +// ); + +// return result; +// });