-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
224 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {z} from "zod"; | ||
|
||
export type ForgotPasswordDto = { | ||
email: string, | ||
} | ||
|
||
export const ForgotPasswordSchema = z.object({ | ||
email: z.string().email("Invalid email!") | ||
}) |
15 changes: 15 additions & 0 deletions
15
src/app/api/auth/forgotpassword/forgot-password.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class ForgotPasswordService { | ||
|
||
/** | ||
* This function will work in a way such that when a user requests | ||
* a password resets, if there's already an existing one, the previous | ||
* request will be invalidated and the new one created and pushed to the | ||
* database. Once the database record has been created, a URL will be | ||
* generated to redirect the user to a page which allows them to reset | ||
* their password. The URL will be "signed", meaning it contains a signed | ||
* JWT token to ensure the integrity of the requests. | ||
*/ | ||
public async sendPasswordResetUrl() { | ||
|
||
} | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import {NextRequest, NextResponse} from "next/server"; | ||
import redis from "@/app/api/utils/redis"; | ||
import Redis from "ioredis"; | ||
import {buildResponse} from "@/app/api/utils/types"; | ||
|
||
export type RateLimiterLocation = { | ||
country: string, | ||
city: string, | ||
} | ||
|
||
export type RateLimiterGeoLimit = { | ||
location: RateLimiterLocation, | ||
limit: number, | ||
duration: number, | ||
} | ||
|
||
export default class RateLimiter { | ||
private readonly redisClient: Redis | ||
|
||
constructor( | ||
private readonly LIMIT_PER_SECOND = 10, | ||
private readonly DURATION = 60, | ||
private readonly GEO_LIMITS: RateLimiterGeoLimit[] = [] | ||
) { | ||
this.redisClient = redis | ||
} | ||
|
||
public async handle(req: NextRequest, work: () => Promise<NextResponse>): Promise<NextResponse> { | ||
const ipAddr = req.ip | ||
if (!ipAddr) | ||
return buildResponse({ | ||
status: 403, | ||
message: "Couldn't fetch your IP address for this rate-limited route!" | ||
}) | ||
|
||
const {country, city} = req.geo ?? {country: undefined, city: undefined} | ||
const matchingLimit = this.GEO_LIMITS.find((limit) => | ||
limit.location.country === country && limit.location.city === city | ||
) | ||
|
||
const doRateLimitCheck = async (key: string, limitMax: number) => { | ||
let count = Number(await this.redisClient.get(key) || 0) | ||
|
||
if (count >= limitMax) | ||
return this.tooManyRequests() | ||
|
||
this.redisClient.incr(key) | ||
this.redisClient.expire(key, this.DURATION) | ||
return work() | ||
} | ||
|
||
if (!matchingLimit) | ||
return doRateLimitCheck(`dreamlogger_${process.env.NODE_ENV}_ratelimit:${ipAddr}`, this.LIMIT_PER_SECOND) | ||
else | ||
return doRateLimitCheck( | ||
`dreamlogger_${process.env.NODE_ENV}_ratelimit:${country}:${city}:${ipAddr}`, | ||
matchingLimit.limit | ||
) | ||
} | ||
|
||
private tooManyRequests(): NextResponse { | ||
return NextResponse.json(null, { | ||
status: 429, | ||
statusText: "You are being rate limited!" | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Redis, {RedisOptions} from "ioredis"; | ||
|
||
const redisOptions: RedisOptions = { | ||
host: process.env.REDIS_HOSTNAME!, | ||
port: parseInt(process.env.REDIS_PORT!), | ||
password: process.env.REDIS_PASSWORD! | ||
} | ||
|
||
const redis = new Redis(redisOptions) | ||
export default redis |