abdellah0l-stack 1.0.2 → 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
package/template/.env.example
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
# Database (Neon PostgreSQL)
|
|
2
2
|
DATABASE_URL=
|
|
3
3
|
|
|
4
|
-
# Node Environment
|
|
5
|
-
NODE_ENV="development"
|
|
6
|
-
|
|
7
4
|
# Better Auth
|
|
8
5
|
BETTER_AUTH_SECRET=
|
|
9
6
|
BETTER_AUTH_URL=http://localhost:3000
|
|
@@ -25,6 +22,3 @@ AI_GATEWAY_API_KEY=
|
|
|
25
22
|
|
|
26
23
|
# UploadThing (File Uploads)
|
|
27
24
|
UPLOADTHING_TOKEN=
|
|
28
|
-
|
|
29
|
-
# App URL
|
|
30
|
-
NEXT_PUBLIC_APP_URL=http://localhost:3000
|
package/template/package.json
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"db:migrate": "drizzle-kit migrate",
|
|
12
12
|
"db:studio": "drizzle-kit studio",
|
|
13
13
|
"db:push": "drizzle-kit push",
|
|
14
|
+
"auth:generate": "npx @better-auth/cli@latest generate --config ./src/lib/auth.ts --output ./src/drizzle/schema/new-auth-schema.ts --yes",
|
|
14
15
|
"lint": "eslint"
|
|
15
16
|
},
|
|
16
17
|
"dependencies": {
|
|
@@ -59,4 +60,4 @@
|
|
|
59
60
|
"tsx": "^4.20.6",
|
|
60
61
|
"typescript": "^5"
|
|
61
62
|
}
|
|
62
|
-
}
|
|
63
|
+
}
|
|
@@ -1,5 +1,105 @@
|
|
|
1
|
-
import { auth } from
|
|
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
|
-
|
|
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
|
|
|
@@ -37,7 +37,7 @@ export const postsRouter = router({
|
|
|
37
37
|
.values({
|
|
38
38
|
title: input.title,
|
|
39
39
|
content: input.content,
|
|
40
|
-
userId: ctx.user.id,
|
|
40
|
+
userId: ctx.session.user.id,
|
|
41
41
|
})
|
|
42
42
|
.returning();
|
|
43
43
|
return post;
|
|
@@ -58,7 +58,7 @@ export const postsRouter = router({
|
|
|
58
58
|
.from(posts)
|
|
59
59
|
.where(eq(posts.id, input.id));
|
|
60
60
|
|
|
61
|
-
if (!existing || existing.userId !== ctx.user.id) {
|
|
61
|
+
if (!existing || existing.userId !== ctx.session.user.id) {
|
|
62
62
|
throw new Error("Not authorized");
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -83,7 +83,7 @@ export const postsRouter = router({
|
|
|
83
83
|
.from(posts)
|
|
84
84
|
.where(eq(posts.id, input.id));
|
|
85
85
|
|
|
86
|
-
if (!existing || existing.userId !== ctx.user.id) {
|
|
86
|
+
if (!existing || existing.userId !== ctx.session.user.id) {
|
|
87
87
|
throw new Error("Not authorized");
|
|
88
88
|
}
|
|
89
89
|
|
|
@@ -3,7 +3,6 @@ import { router, publicProcedure, protectedProcedure } from "../trpc";
|
|
|
3
3
|
import { db } from "@/drizzle/db";
|
|
4
4
|
import { user } from "@/drizzle/schema/auth-schema";
|
|
5
5
|
import { eq } from "drizzle-orm";
|
|
6
|
-
import { session } from '../../drizzle/schema/auth-schema';
|
|
7
6
|
|
|
8
7
|
// this is an example router file for managing users
|
|
9
8
|
// the ctx in protectedProcedure contains the authenticated user's session info
|