abdellah0l-stack 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "abdellah0l-stack",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Scaffold a Next.js project with TypeScript, tRPC, Drizzle, Better-Auth, Arcjet, and Vercel AI SDK",
5
5
  "bin": {
6
6
  "abdellah0l-stack": "./bin/cli.js"
@@ -1,5 +1,105 @@
1
- import { auth } from "@/lib/auth";
2
- import { toNextJsHandler } from "better-auth/next-js";
1
+ import { auth, getSession } from '@/lib/auth';
2
+ import { toNextJsHandler } from "better-auth/next-js"
3
+ import arcjet, {
4
+ BotOptions,
5
+ detectBot,
6
+ EmailOptions,
7
+ protectSignup,
8
+ shield,
9
+ slidingWindow,
10
+ SlidingWindowRateLimitOptions,
11
+ } from "@arcjet/next"
12
+ import { env } from '@/data/env/server';
3
13
 
4
- export const { POST, GET } = toNextJsHandler(auth);
14
+ const aj = arcjet({
15
+ key: env.ARCJET_KEY!,
16
+ characteristics: ["userId"],
17
+ rules: [shield({ mode: "LIVE" })],
18
+ })
19
+
20
+ const botSettings = {
21
+ mode: "LIVE",
22
+ allow: [],
23
+ } satisfies BotOptions
24
+ const restrictiveRateLimitSettings = {
25
+ mode: "LIVE",
26
+ max: 10,
27
+ interval: "10m",
28
+ } satisfies SlidingWindowRateLimitOptions<[]>
29
+ const laxRateLimitSettings = {
30
+ mode: "LIVE",
31
+ max: 60,
32
+ interval: "1m",
33
+ } satisfies SlidingWindowRateLimitOptions<[]>
34
+ const emailSettings = {
35
+ mode: "LIVE",
36
+ block: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"],
37
+ } satisfies EmailOptions
38
+
39
+ const authHandlers = toNextJsHandler(auth)
40
+ export const { GET } = authHandlers
41
+
42
+ export async function POST(request: Request) {
43
+ const clonedRequest = request.clone()
44
+ const decision = await checkArcjet(request)
45
+
46
+ if (decision.isDenied()) {
47
+ if (decision.reason.isRateLimit()) {
48
+ return new Response(null, { status: 429 })
49
+ } else if (decision.reason.isEmail()) {
50
+ let message: string
51
+
52
+ if (decision.reason.emailTypes.includes("INVALID")) {
53
+ message = "Email address format is invalid."
54
+ } else if (decision.reason.emailTypes.includes("DISPOSABLE")) {
55
+ message = "Disposable email addresses are not allowed."
56
+ } else if (decision.reason.emailTypes.includes("NO_MX_RECORDS")) {
57
+ message = "Email domain is not valid."
58
+ } else {
59
+ message = "Invalid email."
60
+ }
61
+
62
+ return Response.json({ message }, { status: 400 })
63
+ } else {
64
+ return new Response(null, { status: 403 })
65
+ }
66
+ }
67
+
68
+ return authHandlers.POST(clonedRequest)
69
+ }
70
+
71
+ async function checkArcjet(request: Request) {
72
+ const body = (await request.json()) as unknown
73
+ const session = await getSession();
74
+ const userId = session?.user.id || "127.0.0.1";
75
+
76
+ if (request.url.endsWith("/auth/sign-up/email")) {
77
+ if (
78
+ body &&
79
+ typeof body === "object" &&
80
+ "email" in body &&
81
+ typeof body.email === "string"
82
+ ) {
83
+ return aj
84
+ .withRule(
85
+ protectSignup({
86
+ email: emailSettings,
87
+ bots: botSettings,
88
+ rateLimit: restrictiveRateLimitSettings,
89
+ })
90
+ )
91
+ .protect(request, { email: body.email, userId })
92
+ } else {
93
+ return aj
94
+ .withRule(detectBot(botSettings))
95
+ .withRule(slidingWindow(restrictiveRateLimitSettings))
96
+ .protect(request, { userId })
97
+ }
98
+ }
99
+
100
+ return aj
101
+ .withRule(detectBot(botSettings))
102
+ .withRule(slidingWindow(laxRateLimitSettings))
103
+ .protect(request, { userId })
104
+ }
5
105