fix middlewares
This commit is contained in:
parent
410a786497
commit
25941e0437
4 changed files with 11 additions and 11 deletions
|
@ -1,7 +1,7 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import { app } from "..";
|
||||
|
||||
app.use('*', (req: Request, res: Response, next: NextFunction) => {
|
||||
app.use('*', (_req: Request, res: Response, next: NextFunction) => {
|
||||
res.header('Access-Control-Allow-Origin', '*');
|
||||
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-auth-user, x-auth-token');
|
||||
res.header('Access-Control-Allow-Methods', '*');
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Request, Response } from "express";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { app, logger } from "..";
|
||||
|
||||
app.use('*', (req: Request, _res: Response, next: () => void) => {
|
||||
app.use('*', (req: Request, _res: Response, next: NextFunction) => {
|
||||
logger.debug(`${req.method} ${req.url}`);
|
||||
next();
|
||||
});
|
||||
|
|
|
@ -20,7 +20,7 @@ class RateLimiter {
|
|||
const ip = req.ip;
|
||||
const reqId = ulid();
|
||||
// ratelimit:ip_address_base64:route_base64
|
||||
const redisKey = `ratelimit:${Buffer.from(ip).toString('base64')}:${Buffer.from(this.route).toString('base64')}`;
|
||||
const redisKey = `ratelimit:${Buffer.from(ip!).toString('base64')}:${Buffer.from(this.route).toString('base64')}`;
|
||||
const reqs = await redis.SCARD(redisKey);
|
||||
if (reqs >= this.limit) {
|
||||
logger.debug(`Ratelimiter: IP address exceeded ratelimit for ${this.route} [${this.limit}/${this.timeframe}]`);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Request } from "express";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { Collection, Db } from "mongodb";
|
||||
import { app, SESSION_LIFETIME } from "..";
|
||||
|
||||
|
@ -8,17 +8,17 @@ export function initializeSessionsMiddleware(db: Db) {
|
|||
sessionsCollection = db.collection('sessions');
|
||||
}
|
||||
|
||||
app.use('*', async (req: Request, next: () => void) => {
|
||||
app.use('*', async (req: Request, _res: Response, next: NextFunction) => {
|
||||
next();
|
||||
const user = req.header('x-auth-user');
|
||||
const token = req.header('x-auth-token');
|
||||
if (!user || !token) return;
|
||||
|
||||
try {
|
||||
const session = await sessionsCollection.findOne({
|
||||
user,
|
||||
token,
|
||||
expires: { $gt: new Date() }
|
||||
const session = await sessionsCollection.findOne({
|
||||
user,
|
||||
token,
|
||||
expires: { $gt: new Date() }
|
||||
});
|
||||
|
||||
if (session) {
|
||||
|
@ -27,7 +27,7 @@ app.use('*', async (req: Request, next: () => void) => {
|
|||
{ $set: { expires: new Date(Date.now() + SESSION_LIFETIME) } }
|
||||
);
|
||||
}
|
||||
} catch(e) {
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue