Skip to content

Commit

Permalink
feat: make redis optional (#333)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmed Elsakaan <ahmedtarek3214@gmail.com>
  • Loading branch information
YoussefLaunchUp and ixahmedxi committed Nov 3, 2023
1 parent 8f29e15 commit d292e24
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/app
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/app

# Redis (Upstash)
# Redis (Upstash) (optional)
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Clerk is our choice of authentication service for Noodle, it's true that it is a

And now you got Auth!

#### Configuring Upstash
#### Configuring Upstash (optional)

1. Create your account through [Upstash's dashboard](https://console.upstash.com)
2. Click on "Create database"
Expand All @@ -179,6 +179,7 @@ And now you got Auth!
6. In the "Connect to your database" section, select "@upstash/redis"
7. Copy the url into your `.env` file as `REDIS_URL` key
8. Copy the token into your `.env` file as `REDIS_TOKEN` key
9. Upstash is only used for ratelimiting, if left empty then ratelimiting will be disabled

And that's all for the redis part!

Expand Down
4 changes: 2 additions & 2 deletions src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const env = createEnv({
NODE_ENV: z
.enum(["development", "test", "production"])
.default("development"),
UPSTASH_REDIS_REST_URL: z.string().min(1),
UPSTASH_REDIS_REST_TOKEN: z.string().min(1),
UPSTASH_REDIS_REST_URL: z.string().min(1).optional(),
UPSTASH_REDIS_REST_TOKEN: z.string().min(0).optional(),
CLERK_SECRET_KEY: z.string().min(1),
OPENWEATHER_API_KEY: z.string().min(1),
},
Expand Down
28 changes: 18 additions & 10 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { authMiddleware } from "@clerk/nextjs";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { type NextRequest, NextResponse } from "next/server";
import { NextResponse, type NextRequest } from "next/server";
import { env } from "./env.mjs";

const redis = new Redis({
url: env.UPSTASH_REDIS_REST_URL,
token: env.UPSTASH_REDIS_REST_TOKEN,
});
let redis: Redis;
let ratelimit: Ratelimit;

const ratelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(20, "3 s"),
});
if (env.UPSTASH_REDIS_REST_URL) {
redis = new Redis({
url: env.UPSTASH_REDIS_REST_URL ?? "",
token: env.UPSTASH_REDIS_REST_TOKEN ?? "",
});

ratelimit = new Ratelimit({
redis: redis,
limiter: Ratelimit.slidingWindow(20, "3 s"),
});
}

async function rateLimitMiddleware(
request: NextRequest,
Expand All @@ -28,7 +33,10 @@ const publicRoutesThatShouldRedirectAfterAuth = ["/", "/waitlist"];

export default authMiddleware({
beforeAuth: (req) => {
return rateLimitMiddleware(req);
if (env.UPSTASH_REDIS_REST_URL) {
return rateLimitMiddleware(req);
}
return NextResponse.next();
},
afterAuth: (auth, req) => {
if (
Expand Down

0 comments on commit d292e24

Please sign in to comment.