create-tigra 3.2.0 → 4.0.0
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/README.md +81 -12
- package/bin/create-tigra.js +495 -472
- package/lib/patchers/email-verification.patcher.js +160 -44
- package/modules/email-verification/client/hooks/useVerification.ts +4 -1
- package/modules/email-verification/server/__tests__/verification.integration.test.ts +115 -0
- package/modules/email-verification/server/verification.controller.ts +3 -1
- package/modules/email-verification/server/verification.service.ts +81 -21
- package/package.json +3 -2
- package/template/.github/dependabot.yml +47 -0
- package/template/.github/workflows/audit.yml +36 -0
- package/template/.github/workflows/ci.yml +79 -0
- package/template/AGENTS.md +18 -9
- package/template/client/.dockerignore +1 -0
- package/template/client/.env.example.production +13 -2
- package/template/client/AGENTS.md +13 -5
- package/template/client/Dockerfile +186 -14
- package/template/client/next.config.ts +1 -1
- package/template/client/package-lock.json +15983 -14181
- package/template/client/package.json +56 -48
- package/template/client/scripts/check-sharp.mjs +53 -0
- package/template/client/scripts/next-dev.cjs +5 -1
- package/template/client/src/__tests__/proxy.test.ts +92 -0
- package/template/client/src/app/healthz/route.ts +16 -0
- package/template/client/src/app/layout.tsx +9 -3
- package/template/client/src/app/page.tsx +2 -2
- package/template/client/src/app/providers.tsx +23 -4
- package/template/client/src/components/layout/Header.tsx +7 -3
- package/template/client/src/features/admin/hooks/useAdminSessions.ts +2 -1
- package/template/client/src/features/admin/hooks/useAdminUsers.ts +3 -1
- package/template/client/src/features/admin/types/admin.types.ts +2 -3
- package/template/client/src/features/auth/components/AuthInitializer.tsx +38 -19
- package/template/client/src/features/auth/components/ForgotPasswordForm.tsx +107 -0
- package/template/client/src/features/auth/components/LimitNotice.tsx +28 -0
- package/template/client/src/features/auth/components/LoginForm.tsx +18 -4
- package/template/client/src/features/auth/components/RegisterForm.tsx +23 -7
- package/template/client/src/features/auth/hooks/useAuth.ts +27 -7
- package/template/client/src/features/auth/hooks/usePasswordReset.ts +7 -1
- package/template/client/src/features/auth/services/auth.service.ts +8 -3
- package/template/client/src/features/auth/store/authSlice.ts +7 -2
- package/template/client/src/features/auth/types/auth.types.ts +3 -0
- package/template/client/src/instrumentation-client.ts +9 -1
- package/template/client/src/instrumentation.ts +6 -0
- package/template/client/src/lib/api/__tests__/axios.config.test.ts +183 -0
- package/template/client/src/lib/api/api.types.ts +14 -0
- package/template/client/src/lib/api/axios.config.ts +101 -15
- package/template/client/src/lib/api/hard-redirect.ts +8 -0
- package/template/client/src/lib/api/session-events.ts +29 -0
- package/template/client/src/lib/constants/routes.ts +12 -0
- package/template/client/src/lib/observability/sentry-scrub.ts +71 -0
- package/template/client/src/lib/utils/__tests__/error.test.ts +119 -0
- package/template/client/src/lib/utils/__tests__/form.test.ts +42 -0
- package/template/client/src/lib/utils/__tests__/paths.test.ts +43 -0
- package/template/client/src/lib/utils/__tests__/security.test.ts +40 -0
- package/template/client/src/lib/utils/__tests__/session-marker.test.ts +27 -0
- package/template/client/src/lib/utils/error.ts +86 -1
- package/template/client/src/lib/utils/form.ts +31 -0
- package/template/client/src/lib/utils/paths.ts +18 -0
- package/template/client/src/lib/utils/security.ts +37 -0
- package/template/client/src/lib/utils/session-marker.ts +24 -0
- package/template/client/src/{middleware.ts → proxy.ts} +25 -11
- package/template/client/src/store/__tests__/store.test.ts +44 -0
- package/template/client/src/store/index.ts +26 -8
- package/template/client/vitest.config.mts +48 -0
- package/template/server/.env.example +280 -269
- package/template/server/.env.example.production +283 -236
- package/template/server/AGENTS.md +13 -4
- package/template/server/Dockerfile +181 -14
- package/template/server/docker-compose.yml +4 -2
- package/template/server/docs/deployment.md +63 -0
- package/template/server/docs/logging.md +52 -4
- package/template/server/docs/rate-limits.md +153 -0
- package/template/server/docs/testing.md +63 -0
- package/template/server/eslint.config.mjs +19 -0
- package/template/server/package-lock.json +8784 -8743
- package/template/server/package.json +79 -75
- package/template/server/prisma/migrations/0_init/migration.sql +82 -0
- package/template/server/prisma/migrations/migration_lock.toml +3 -0
- package/template/server/prisma/schema.prisma +19 -6
- package/template/server/prisma/seed.ts +13 -5
- package/template/server/scripts/check-heic.mjs +40 -0
- package/template/server/scripts/check-sharp.mjs +53 -0
- package/template/server/src/__tests__/routes.integration.test.ts +280 -0
- package/template/server/src/app.ts +248 -335
- package/template/server/src/config/__tests__/env.test.ts +219 -0
- package/template/server/src/config/__tests__/rate-limit.config.test.ts +158 -0
- package/template/server/src/config/env.ts +326 -171
- package/template/server/src/config/rate-limit.config.ts +157 -106
- package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +110 -0
- package/template/server/src/jobs/__tests__/cleanup-expired-auth.job.test.ts +44 -0
- package/template/server/src/jobs/__tests__/jobs.integration.test.ts +88 -0
- package/template/server/src/jobs/__tests__/schedule.test.ts +92 -0
- package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +81 -65
- package/template/server/src/jobs/cleanup-expired-auth.job.ts +32 -24
- package/template/server/src/jobs/index.ts +12 -2
- package/template/server/src/jobs/refresh-cloudflare-ranges.job.ts +28 -0
- package/template/server/src/jobs/schedule.ts +50 -0
- package/template/server/src/libs/__tests__/auth.test.ts +168 -0
- package/template/server/src/libs/__tests__/client-ip.test.ts +257 -121
- package/template/server/src/libs/__tests__/cloudflare-ranges.test.ts +79 -0
- package/template/server/src/libs/__tests__/cookies.test.ts +113 -0
- package/template/server/src/libs/__tests__/email-quota.test.ts +81 -0
- package/template/server/src/libs/__tests__/email.test.ts +68 -0
- package/template/server/src/libs/__tests__/error-handler.test.ts +162 -0
- package/template/server/src/libs/__tests__/health-db-down.integration.test.ts +37 -0
- package/template/server/src/libs/__tests__/health-redis-down.integration.test.ts +45 -0
- package/template/server/src/libs/__tests__/health-routes.test.ts +83 -0
- package/template/server/src/libs/__tests__/health.integration.test.ts +118 -0
- package/template/server/src/libs/__tests__/http.test.ts +74 -2
- package/template/server/src/libs/__tests__/ip-block.test.ts +179 -62
- package/template/server/src/libs/__tests__/logger.test.ts +50 -0
- package/template/server/src/libs/__tests__/monitoring.test.ts +185 -0
- package/template/server/src/libs/__tests__/outbound.integration.test.ts +48 -0
- package/template/server/src/libs/__tests__/prisma-client.test.ts +28 -0
- package/template/server/src/libs/__tests__/query-counter.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/rate-limiting.test.ts +146 -0
- package/template/server/src/libs/__tests__/rate-limits.integration.test.ts +53 -0
- package/template/server/src/libs/__tests__/redis-required.test.ts +30 -0
- package/template/server/src/libs/__tests__/redis.test.ts +59 -0
- package/template/server/src/libs/__tests__/request-id.test.ts +25 -0
- package/template/server/src/libs/__tests__/requestLogger.test.ts +126 -0
- package/template/server/src/libs/__tests__/safe-http.test.ts +138 -0
- package/template/server/src/libs/__tests__/sentry-scrub.test.ts +82 -0
- package/template/server/src/libs/__tests__/shutdown.test.ts +89 -0
- package/template/server/src/libs/__tests__/url-safety.test.ts +130 -80
- package/template/server/src/libs/account-status.ts +52 -0
- package/template/server/src/libs/auth.ts +51 -12
- package/template/server/src/libs/client-ip.ts +158 -77
- package/template/server/src/libs/cloudflare-ranges.ts +132 -0
- package/template/server/src/libs/cookies.ts +91 -24
- package/template/server/src/libs/email-quota.ts +74 -0
- package/template/server/src/libs/email.ts +46 -9
- package/template/server/src/libs/error-handler.ts +111 -0
- package/template/server/src/libs/health-routes.ts +53 -0
- package/template/server/src/libs/http.ts +139 -47
- package/template/server/src/libs/ip-address.ts +37 -0
- package/template/server/src/libs/ip-block.ts +284 -220
- package/template/server/src/libs/logger.ts +41 -12
- package/template/server/src/libs/monitoring.ts +131 -42
- package/template/server/src/libs/observability/sentry.ts +80 -0
- package/template/server/src/libs/origin-check.ts +9 -7
- package/template/server/src/libs/prisma.ts +47 -4
- package/template/server/src/libs/query-counter.ts +71 -59
- package/template/server/src/libs/rate-limiting.ts +110 -0
- package/template/server/src/libs/redis-required.ts +28 -0
- package/template/server/src/libs/redis.ts +108 -18
- package/template/server/src/libs/request-id.ts +18 -0
- package/template/server/src/libs/requestLogger.ts +112 -17
- package/template/server/src/libs/safe-http.ts +101 -0
- package/template/server/src/libs/shutdown.ts +87 -0
- package/template/server/src/libs/storage/__tests__/fixtures/tiny-heic.ts +13 -0
- package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +143 -0
- package/template/server/src/libs/storage/__tests__/multipart-limits.test.ts +91 -0
- package/template/server/src/libs/storage/__tests__/upload-persistence.test.ts +52 -0
- package/template/server/src/libs/storage/file-storage.service.ts +104 -83
- package/template/server/src/libs/storage/file-validator.ts +48 -46
- package/template/server/src/libs/storage/image-optimizer.service.ts +78 -72
- package/template/server/src/libs/token-hash.ts +18 -0
- package/template/server/src/libs/url-safety.ts +127 -121
- package/template/server/src/libs/validation-errors.ts +86 -0
- package/template/server/src/modules/admin/__tests__/admin-actions.integration.test.ts +255 -0
- package/template/server/src/modules/admin/__tests__/admin.health.test.ts +82 -0
- package/template/server/src/modules/admin/__tests__/admin.repo.test.ts +55 -0
- package/template/server/src/modules/admin/__tests__/admin.schemas.test.ts +38 -0
- package/template/server/src/modules/admin/__tests__/admin.service.test.ts +162 -0
- package/template/server/src/modules/admin/admin.controller.ts +66 -49
- package/template/server/src/modules/admin/admin.repo.ts +56 -13
- package/template/server/src/modules/admin/admin.routes.ts +48 -2
- package/template/server/src/modules/admin/admin.schemas.ts +18 -6
- package/template/server/src/modules/admin/admin.service.ts +73 -7
- package/template/server/src/modules/auth/__tests__/auth.controller.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/auth.integration.test.ts +9 -3
- package/template/server/src/modules/auth/__tests__/auth.service.dummy-hash.test.ts +34 -0
- package/template/server/src/modules/auth/__tests__/auth.service.test.ts +332 -49
- package/template/server/src/modules/auth/__tests__/session-cleanup.test.ts +39 -0
- package/template/server/src/modules/auth/__tests__/sessions.integration.test.ts +246 -0
- package/template/server/src/modules/auth/__tests__/token-fingerprints.test.ts +119 -0
- package/template/server/src/modules/auth/auth.controller.ts +133 -128
- package/template/server/src/modules/auth/auth.repo.ts +60 -42
- package/template/server/src/modules/auth/auth.schemas.ts +5 -2
- package/template/server/src/modules/auth/auth.service.ts +197 -109
- package/template/server/src/modules/auth/session.repo.ts +12 -12
- package/template/server/src/modules/files/__tests__/files.integration.test.ts +5 -5
- package/template/server/src/modules/users/__tests__/avatar-upload.test.ts +120 -0
- package/template/server/src/modules/users/__tests__/name-rules.test.ts +46 -0
- package/template/server/src/modules/users/__tests__/users.integration.test.ts +295 -0
- package/template/server/src/modules/users/__tests__/users.repo.test.ts +37 -0
- package/template/server/src/modules/users/__tests__/users.service.test.ts +129 -0
- package/template/server/src/modules/users/users.controller.ts +9 -38
- package/template/server/src/modules/users/users.repo.ts +25 -7
- package/template/server/src/modules/users/users.routes.ts +3 -22
- package/template/server/src/modules/users/users.schemas.ts +15 -9
- package/template/server/src/modules/users/users.service.ts +105 -93
- package/template/server/src/scripts/make-admin.ts +48 -0
- package/template/server/src/server.ts +48 -26
- package/template/server/src/shared/errors/AppError.ts +27 -1
- package/template/server/src/shared/errors/errors.ts +42 -5
- package/template/server/src/shared/responses/successResponse.ts +22 -3
- package/template/server/src/shared/types/index.ts +7 -0
- package/template/server/src/test/__tests__/integration-services.test.ts +66 -0
- package/template/server/src/test/integration-helpers.ts +343 -0
- package/template/server/src/test/integration-services.ts +74 -0
- package/template/server/src/test/integration.global-setup.ts +144 -0
- package/template/server/src/test/integration.setup.ts +35 -158
- package/template/server/src/test/setup.ts +23 -8
- package/template/server/tsconfig.build.json +1 -1
- package/template/server/tsconfig.json +1 -0
- package/template/server/vitest.config.ts +17 -4
- package/template/server/vitest.integration.config.ts +18 -13
- package/template/server/src/libs/storage/filename-sanitizer.ts +0 -71
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { prisma, isPrismaNotFound } from '@libs/prisma.js';
|
|
1
|
+
import { prisma, isPrismaNotFound, isPrismaWriteConflict } from '@libs/prisma.js';
|
|
2
|
+
import { hashToken } from '@libs/token-hash.js';
|
|
2
3
|
import type { User, RefreshToken } from '@prisma/client';
|
|
3
4
|
|
|
4
5
|
export async function findUserByEmail(email: string): Promise<User | null> {
|
|
@@ -24,13 +25,18 @@ export async function createUser(data: {
|
|
|
24
25
|
password: string;
|
|
25
26
|
firstName: string;
|
|
26
27
|
lastName: string;
|
|
27
|
-
|
|
28
|
+
/** null = must verify email first; a Date = already verified. */
|
|
29
|
+
emailVerifiedAt?: Date | null;
|
|
28
30
|
}): Promise<User> {
|
|
29
31
|
return prisma.user.create({
|
|
30
32
|
data,
|
|
31
33
|
});
|
|
32
34
|
}
|
|
33
35
|
|
|
36
|
+
// Refresh tokens: callers pass the RAW token (from the cookie); the table only
|
|
37
|
+
// ever holds its fingerprint (libs/token-hash.ts), so reading the table gives
|
|
38
|
+
// nobody a usable login. Every function below hashes before touching it.
|
|
39
|
+
|
|
34
40
|
export async function createRefreshToken(data: {
|
|
35
41
|
token: string;
|
|
36
42
|
userId: string;
|
|
@@ -38,20 +44,20 @@ export async function createRefreshToken(data: {
|
|
|
38
44
|
expiresAt: Date;
|
|
39
45
|
}): Promise<RefreshToken> {
|
|
40
46
|
return prisma.refreshToken.create({
|
|
41
|
-
data,
|
|
47
|
+
data: { ...data, token: hashToken(data.token) },
|
|
42
48
|
});
|
|
43
49
|
}
|
|
44
50
|
|
|
45
51
|
export async function findRefreshToken(token: string): Promise<RefreshToken | null> {
|
|
46
52
|
return prisma.refreshToken.findUnique({
|
|
47
|
-
where: { token },
|
|
53
|
+
where: { token: hashToken(token) },
|
|
48
54
|
});
|
|
49
55
|
}
|
|
50
56
|
|
|
51
57
|
export async function deleteRefreshToken(token: string): Promise<void> {
|
|
52
58
|
try {
|
|
53
59
|
await prisma.refreshToken.delete({
|
|
54
|
-
where: { token },
|
|
60
|
+
where: { token: hashToken(token) },
|
|
55
61
|
});
|
|
56
62
|
} catch (error) {
|
|
57
63
|
if (isPrismaNotFound(error)) return;
|
|
@@ -59,53 +65,65 @@ export async function deleteRefreshToken(token: string): Promise<void> {
|
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Atomically swap a refresh token for a new one AND move its session forward.
|
|
70
|
+
*
|
|
71
|
+
* The session row is what users (GET /auth/sessions) and admins see and
|
|
72
|
+
* revoke, so it must stay in step with the token family: every rotation sets
|
|
73
|
+
* the session's expiresAt to the new token's expiry and refreshes lastActiveAt.
|
|
74
|
+
* Previously only the token's expiry slid forward, so after the first
|
|
75
|
+
* JWT_REFRESH_EXPIRY window a still-used login vanished from session lists and
|
|
76
|
+
* could no longer be force-expired.
|
|
77
|
+
*
|
|
78
|
+
* Exactly one of several parallel rotations of the same token wins. The old
|
|
79
|
+
* row is removed with deleteMany, a locking DELETE whose count says whether
|
|
80
|
+
* THIS transaction removed it: a parallel one waits for the row lock, then
|
|
81
|
+
* removes nothing and returns false. (delete() checked the row in a snapshot
|
|
82
|
+
* first, so under MySQL's REPEATABLE READ two parallel refreshes both "found"
|
|
83
|
+
* it and both created a new token, forking the login; sometimes they
|
|
84
|
+
* deadlocked instead and answered 500.)
|
|
85
|
+
*
|
|
86
|
+
* Returns false when this request did not win: the token was already rotated
|
|
87
|
+
* by a parallel refresh, the session is gone, or MySQL reported a write
|
|
88
|
+
* conflict with the parallel transaction. See refresh() in auth.service.ts.
|
|
89
|
+
* Both tokens are passed raw and stored as fingerprints.
|
|
90
|
+
*/
|
|
62
91
|
export async function rotateRefreshToken(
|
|
63
92
|
oldToken: string,
|
|
64
|
-
newData: { token: string; userId: string; sessionId
|
|
93
|
+
newData: { token: string; userId: string; sessionId: string; expiresAt: Date },
|
|
65
94
|
): Promise<boolean> {
|
|
66
95
|
try {
|
|
67
|
-
await prisma.$transaction(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
96
|
+
return await prisma.$transaction(async (tx) => {
|
|
97
|
+
const { count } = await tx.refreshToken.deleteMany({ where: { token: hashToken(oldToken) } });
|
|
98
|
+
if (count !== 1) return false; // a parallel refresh rotated it first
|
|
99
|
+
await tx.session.update({
|
|
100
|
+
where: { id: newData.sessionId },
|
|
101
|
+
data: { expiresAt: newData.expiresAt, lastActiveAt: new Date() },
|
|
102
|
+
});
|
|
103
|
+
await tx.refreshToken.create({ data: { ...newData, token: hashToken(newData.token) } });
|
|
104
|
+
return true;
|
|
105
|
+
});
|
|
72
106
|
} catch (error) {
|
|
73
|
-
if (isPrismaNotFound(error)) return false;
|
|
107
|
+
if (isPrismaNotFound(error) || isPrismaWriteConflict(error)) return false;
|
|
74
108
|
throw error;
|
|
75
109
|
}
|
|
76
110
|
}
|
|
77
111
|
|
|
78
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Delete a user's refresh tokens; with `exceptSessionId`, keep that one
|
|
114
|
+
* session's tokens (an owner's password change keeps the asking device signed
|
|
115
|
+
* in). Tokens without a session are always deleted: the explicit `sessionId:
|
|
116
|
+
* null` branch is needed because SQL `sessionId <> x` skips NULL rows.
|
|
117
|
+
*/
|
|
118
|
+
export async function deleteRefreshTokensByUserId(
|
|
119
|
+
userId: string,
|
|
120
|
+
options: { exceptSessionId?: string } = {},
|
|
121
|
+
): Promise<void> {
|
|
122
|
+
const { exceptSessionId } = options;
|
|
79
123
|
await prisma.refreshToken.deleteMany({
|
|
80
|
-
where:
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** @deprecated Lockout moved to Redis email+IP keys in auth.service.ts; kept for back-compat, slated for removal. */
|
|
85
|
-
export async function incrementFailedAttempts(userId: string): Promise<void> {
|
|
86
|
-
await prisma.user.update({
|
|
87
|
-
where: { id: userId },
|
|
88
|
-
data: {
|
|
89
|
-
failedLoginAttempts: { increment: 1 },
|
|
90
|
-
},
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** @deprecated Lockout moved to Redis email+IP keys in auth.service.ts; kept for back-compat, slated for removal. */
|
|
95
|
-
export async function setAccountLock(userId: string, lockedUntil: Date): Promise<void> {
|
|
96
|
-
await prisma.user.update({
|
|
97
|
-
where: { id: userId },
|
|
98
|
-
data: { lockedUntil },
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export async function resetFailedAttempts(userId: string): Promise<void> {
|
|
103
|
-
await prisma.user.update({
|
|
104
|
-
where: { id: userId },
|
|
105
|
-
data: {
|
|
106
|
-
failedLoginAttempts: 0,
|
|
107
|
-
lockedUntil: null,
|
|
108
|
-
},
|
|
124
|
+
where: exceptSessionId
|
|
125
|
+
? { userId, OR: [{ sessionId: null }, { sessionId: { not: exceptSessionId } }] }
|
|
126
|
+
: { userId },
|
|
109
127
|
});
|
|
110
128
|
}
|
|
111
129
|
|
|
@@ -9,8 +9,10 @@ export const registerSchema = z.object({
|
|
|
9
9
|
.regex(/[A-Z]/, 'Password must contain at least one uppercase letter')
|
|
10
10
|
.regex(/[a-z]/, 'Password must contain at least one lowercase letter')
|
|
11
11
|
.regex(/[0-9]/, 'Password must contain at least one number'),
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
// trim() FIRST: Zod 4 runs the steps in order, so a length check before the
|
|
13
|
+
// trim let " " through and saved it as an empty name.
|
|
14
|
+
firstName: z.string().trim().min(2, 'First name must be at least 2 characters').max(100),
|
|
15
|
+
lastName: z.string().trim().min(2, 'Last name must be at least 2 characters').max(100),
|
|
14
16
|
});
|
|
15
17
|
|
|
16
18
|
export const loginSchema = z.object({
|
|
@@ -48,6 +50,7 @@ export const userSchema = z.object({
|
|
|
48
50
|
avatarUrl: z.string().nullable(),
|
|
49
51
|
role: z.enum(['USER', 'ADMIN']),
|
|
50
52
|
isActive: z.boolean(),
|
|
53
|
+
emailVerifiedAt: z.string().nullable(),
|
|
51
54
|
createdAt: z.string().datetime(),
|
|
52
55
|
updatedAt: z.string().datetime(),
|
|
53
56
|
});
|