mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-09 20:29:36 +02:00
Update passport/prisma libs
This commit is contained in:
@@ -9,16 +9,30 @@ import {
|
|||||||
import { Strategy as LocalStrategy } from 'passport-local';
|
import { Strategy as LocalStrategy } from 'passport-local';
|
||||||
import { prisma } from './prisma';
|
import { prisma } from './prisma';
|
||||||
|
|
||||||
export const generateToken = (userId: string) => {
|
export const generateToken = (
|
||||||
return jwt.sign({ id: userId }, String(process.env.TOKEN_SECRET), {
|
id: string,
|
||||||
|
otherProperties?: { [key: string]: any }
|
||||||
|
) => {
|
||||||
|
return jwt.sign(
|
||||||
|
{ id, ...otherProperties },
|
||||||
|
String(process.env.TOKEN_SECRET),
|
||||||
|
{
|
||||||
expiresIn: String(process.env.TOKEN_EXPIRATION || '15m'),
|
expiresIn: String(process.env.TOKEN_EXPIRATION || '15m'),
|
||||||
});
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const generateRefreshToken = (userId: string) => {
|
export const generateRefreshToken = (
|
||||||
return jwt.sign({ id: userId }, String(process.env.TOKEN_SECRET), {
|
id: string,
|
||||||
|
otherProperties?: { [key: string]: any }
|
||||||
|
) => {
|
||||||
|
return jwt.sign(
|
||||||
|
{ id, ...otherProperties },
|
||||||
|
String(process.env.TOKEN_SECRET),
|
||||||
|
{
|
||||||
expiresIn: String(process.env.TOKEN_REFRESH_EXPIRATION || '90d'),
|
expiresIn: String(process.env.TOKEN_REFRESH_EXPIRATION || '90d'),
|
||||||
});
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const authenticateUser = async (
|
const authenticateUser = async (
|
||||||
@@ -55,7 +69,6 @@ passport.use(
|
|||||||
await prisma.user
|
await prisma.user
|
||||||
.findUnique({
|
.findUnique({
|
||||||
include: {
|
include: {
|
||||||
serverCredentials: true,
|
|
||||||
serverFolderPermissions: true,
|
serverFolderPermissions: true,
|
||||||
serverPermissions: true,
|
serverPermissions: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { PrismaClient } from '@prisma/client';
|
import { Prisma, PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
export const prisma = new PrismaClient({ errorFormat: 'minimal' });
|
export const prisma = new PrismaClient({ errorFormat: 'minimal' });
|
||||||
export const exclude = <T, Key extends keyof T>(
|
export const exclude = <T, Key extends keyof T>(
|
||||||
@@ -17,7 +17,7 @@ function sleep(ms: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
prisma.$use(async (params, next) => {
|
prisma.$use(async (params, next) => {
|
||||||
const maxRetries = 5;
|
const maxRetries = 3;
|
||||||
let retries = 0;
|
let retries = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -25,8 +25,29 @@ prisma.$use(async (params, next) => {
|
|||||||
const result = await next(params);
|
const result = await next(params);
|
||||||
return result;
|
return result;
|
||||||
} catch (err) {
|
} 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;
|
retries += 1;
|
||||||
return sleep(500);
|
return sleep(100);
|
||||||
}
|
}
|
||||||
} while (retries < maxRetries);
|
} 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;
|
||||||
|
// });
|
||||||
|
|||||||
Reference in New Issue
Block a user