Move server directory outside of frontend src

This commit is contained in:
jeffvli
2022-10-25 16:52:45 -07:00
parent 863dce88b7
commit 0438f2d5f2
105 changed files with 16946 additions and 6901 deletions
+2
View File
@@ -0,0 +1,2 @@
export * from './prisma';
export { default as throttle } from './throttle';
+100
View File
@@ -0,0 +1,100 @@
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import passport from 'passport';
import {
Strategy as JwtStrategy,
ExtractJwt,
StrategyOptions,
} from 'passport-jwt';
import { Strategy as LocalStrategy } from 'passport-local';
import { prisma } from './prisma';
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 = (
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 (
username: string,
password: string,
done: any
) => {
const user = await prisma.user.findUnique({ where: { username } });
if (user === null || user === undefined) {
return done(null, false);
}
if (!user.enabled) {
return done(null, false, { message: 'The user is not enabled.' });
}
if (await bcrypt.compare(password, user.password)) {
return done(null, user);
}
return done(null, false, { message: 'Invalid credentials.' });
};
passport.use(new LocalStrategy(authenticateUser));
const jwtOptions: StrategyOptions = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: String(process.env.TOKEN_SECRET),
};
passport.use(
new JwtStrategy(jwtOptions, async (jwt_payload: any, done: any) => {
await prisma.user
.findUnique({
include: {
serverFolderPermissions: true,
serverPermissions: true,
},
where: { id: jwt_payload.id },
})
.then((user) => {
// eslint-disable-next-line promise/no-callback-in-promise
return done(null, user);
})
.catch((err) => {
console.log(err.message);
});
})
);
passport.serializeUser((user: any, done) => {
return done(null, user.id);
});
passport.deserializeUser(async (id: string, done) => {
return done(
null,
await prisma.user.findUnique({
where: {
id,
},
})
);
});
+53
View File
@@ -0,0 +1,53 @@
import { Prisma, PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient({ errorFormat: 'minimal' });
export const exclude = <T, Key extends keyof T>(
resultSet: T,
...keys: Key[]
): Omit<T, Key> => {
// eslint-disable-next-line no-restricted-syntax
for (const key of keys) {
delete resultSet[key];
}
return resultSet;
};
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
prisma.$use(async (params, next) => {
const maxRetries = 3;
let retries = 0;
do {
try {
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(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;
// });
+8
View File
@@ -0,0 +1,8 @@
import pThrottle from 'p-throttle';
const throttle = pThrottle({
interval: 1000,
limit: 10,
});
export default throttle;