Skip to content

Commit

Permalink
r010 [DPTM-12] (#62)
Browse files Browse the repository at this point in the history
* ar(fix) session req

* ar(fix) session req

* ar(fix) disable kv (cost)

* ar(fix) disable kv (cost)

* ar(fix) disable kv (cost)
  • Loading branch information
angeloreale authored Aug 31, 2024
1 parent 2c867e0 commit d756dbf
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { next } from '@vercel/edge';
import { ipAddress } from '@vercel/functions';
import { kv } from '@vercel/kv';
import { Ratelimit } from '@upstash/ratelimit';
// import { ipAddress } from '@vercel/functions';
// import { kv } from '@vercel/kv';
// import { Ratelimit } from '@upstash/ratelimit';

const ratelimit = new Ratelimit({
redis: kv,
limiter: Ratelimit.slidingWindow(10, '3 s'),
});
// const ratelimit = new Ratelimit({
// redis: kv,
// limiter: Ratelimit.slidingWindow(10, '3 s'),
// });

export const config = {
matcher: ['/api/:path*'],
Expand All @@ -37,19 +37,18 @@ export default async function middleware(request: NextRequest) {
if (origin !== process.env.MAIN_URL) {
headers['Access-Control-Allow-Origin'] = allowedOrigins[origin] || 'https://www.dreampip.com';
}

// You could alternatively limit based on user ID or similar
const response = next();
const ip = ipAddress(request) || '127.0.0.1';

const { success, pending, limit, reset, remaining } = await ratelimit.limit(ip);
// You could alternatively limit based on user ID or similar
// const ip = ipAddress(request) || '127.0.0.1';
// const { success, pending, limit, reset, remaining } = await ratelimit.limit(ip);

if (!request?.url?.includes('auth')) {
Object.keys(headers).forEach((key: string) => {
response.headers.set(key, headers[key]);
});
}

return success ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
// return response ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
// return success ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
return response ? response : NextResponse.redirect(new URL('https://www.dreampip.com/404', request.url));
}

0 comments on commit d756dbf

Please sign in to comment.