create-tigra 3.1.1 → 3.3.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 +1 -1
- package/bin/create-tigra.js +6 -5
- package/lib/patchers/email-verification.patcher.js +93 -39
- package/modules/email-verification/server/verification.service.ts +38 -9
- package/package.json +1 -1
- package/template/.claude/skills/security-audit/SKILL.md +10 -0
- package/template/AGENTS.md +18 -2
- package/template/CLAUDE.md +1 -0
- package/template/client/CLAUDE.md +1 -0
- package/template/client/src/features/auth/components/AuthInitializer.tsx +4 -2
- package/template/client/src/features/auth/components/LoginForm.tsx +5 -2
- package/template/client/src/features/auth/hooks/useAuth.ts +16 -6
- 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/observability/sentry-scrub.ts +71 -0
- package/template/client/src/lib/utils/error.ts +5 -1
- package/template/client/src/lib/utils/security.ts +37 -0
- package/template/server/.env.example +5 -4
- package/template/server/.env.example.production +14 -8
- package/template/server/CLAUDE.md +1 -0
- package/template/server/Dockerfile +5 -0
- package/template/server/prisma/schema.prisma +19 -3
- package/template/server/prisma/seed.ts +13 -5
- package/template/server/src/app.ts +11 -0
- package/template/server/src/config/env.ts +17 -6
- package/template/server/src/jobs/__tests__/cleanup-deleted-accounts.job.test.ts +58 -0
- package/template/server/src/jobs/cleanup-deleted-accounts.job.ts +72 -58
- package/template/server/src/libs/__tests__/auth.test.ts +77 -0
- package/template/server/src/libs/__tests__/cookies.test.ts +62 -0
- package/template/server/src/libs/__tests__/redis.test.ts +23 -0
- package/template/server/src/libs/__tests__/sentry-scrub.test.ts +80 -0
- package/template/server/src/libs/account-status.ts +52 -0
- package/template/server/src/libs/auth.ts +15 -5
- package/template/server/src/libs/cookies.ts +40 -12
- package/template/server/src/libs/ip-block.ts +3 -2
- package/template/server/src/libs/monitoring.ts +4 -1
- package/template/server/src/libs/observability/sentry.ts +80 -0
- package/template/server/src/libs/redis.ts +82 -18
- package/template/server/src/libs/storage/__tests__/image-optimizer.test.ts +84 -0
- package/template/server/src/libs/storage/file-storage.service.ts +21 -10
- package/template/server/src/libs/storage/image-optimizer.service.ts +67 -72
- package/template/server/src/modules/admin/admin.repo.ts +7 -3
- package/template/server/src/modules/admin/admin.service.ts +7 -3
- package/template/server/src/modules/auth/__tests__/auth.service.test.ts +136 -13
- package/template/server/src/modules/auth/auth.controller.ts +1 -1
- package/template/server/src/modules/auth/auth.repo.ts +20 -2
- package/template/server/src/modules/auth/auth.schemas.ts +1 -0
- package/template/server/src/modules/auth/auth.service.ts +30 -13
- package/template/server/src/modules/auth/session.repo.ts +0 -12
- package/template/server/src/modules/users/users.repo.ts +2 -0
- package/template/server/src/test/setup.ts +22 -0
|
@@ -24,7 +24,8 @@ export async function createUser(data: {
|
|
|
24
24
|
password: string;
|
|
25
25
|
firstName: string;
|
|
26
26
|
lastName: string;
|
|
27
|
-
|
|
27
|
+
/** null = must verify email first; a Date = already verified. */
|
|
28
|
+
emailVerifiedAt?: Date | null;
|
|
28
29
|
}): Promise<User> {
|
|
29
30
|
return prisma.user.create({
|
|
30
31
|
data,
|
|
@@ -59,13 +60,30 @@ export async function deleteRefreshToken(token: string): Promise<void> {
|
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Atomically swap a refresh token for a new one AND move its session forward.
|
|
65
|
+
*
|
|
66
|
+
* The session row is what users (GET /auth/sessions) and admins see and
|
|
67
|
+
* revoke, so it must stay in step with the token family: every rotation sets
|
|
68
|
+
* the session's expiresAt to the new token's expiry and refreshes lastActiveAt.
|
|
69
|
+
* Previously only the token's expiry slid forward, so after the first
|
|
70
|
+
* JWT_REFRESH_EXPIRY window a still-used login vanished from session lists and
|
|
71
|
+
* could no longer be force-expired.
|
|
72
|
+
*
|
|
73
|
+
* Returns false when the old token (or the session) is already gone — a
|
|
74
|
+
* concurrent redemption, which the caller treats as reuse.
|
|
75
|
+
*/
|
|
62
76
|
export async function rotateRefreshToken(
|
|
63
77
|
oldToken: string,
|
|
64
|
-
newData: { token: string; userId: string; sessionId
|
|
78
|
+
newData: { token: string; userId: string; sessionId: string; expiresAt: Date },
|
|
65
79
|
): Promise<boolean> {
|
|
66
80
|
try {
|
|
67
81
|
await prisma.$transaction([
|
|
68
82
|
prisma.refreshToken.delete({ where: { token: oldToken } }),
|
|
83
|
+
prisma.session.update({
|
|
84
|
+
where: { id: newData.sessionId },
|
|
85
|
+
data: { expiresAt: newData.expiresAt, lastActiveAt: new Date() },
|
|
86
|
+
}),
|
|
69
87
|
prisma.refreshToken.create({ data: newData }),
|
|
70
88
|
]);
|
|
71
89
|
return true;
|
|
@@ -5,10 +5,10 @@ import { getRedis } from '@libs/redis.js';
|
|
|
5
5
|
import { sendEmail } from '@libs/email.js';
|
|
6
6
|
import { logger } from '@libs/logger.js';
|
|
7
7
|
import { env } from '@config/env.js';
|
|
8
|
+
import { assertAccountCanSignIn } from '@libs/account-status.js';
|
|
8
9
|
import {
|
|
9
10
|
ConflictError,
|
|
10
11
|
UnauthorizedError,
|
|
11
|
-
ForbiddenError,
|
|
12
12
|
NotFoundError,
|
|
13
13
|
BadRequestError,
|
|
14
14
|
} from '@shared/errors/errors.js';
|
|
@@ -106,6 +106,8 @@ interface SanitizedUser {
|
|
|
106
106
|
avatarUrl: string | null;
|
|
107
107
|
role: UserRole;
|
|
108
108
|
isActive: boolean;
|
|
109
|
+
/** ISO timestamp of email verification, or null while unverified. */
|
|
110
|
+
emailVerifiedAt: string | null;
|
|
109
111
|
createdAt: string;
|
|
110
112
|
updatedAt: string;
|
|
111
113
|
}
|
|
@@ -131,6 +133,7 @@ function sanitizeUser(user: {
|
|
|
131
133
|
avatarUrl?: string | null;
|
|
132
134
|
role: UserRole;
|
|
133
135
|
isActive: boolean;
|
|
136
|
+
emailVerifiedAt: Date | null;
|
|
134
137
|
createdAt: Date;
|
|
135
138
|
updatedAt: Date;
|
|
136
139
|
}): SanitizedUser {
|
|
@@ -142,6 +145,7 @@ function sanitizeUser(user: {
|
|
|
142
145
|
avatarUrl: user.avatarUrl ?? null,
|
|
143
146
|
role: user.role,
|
|
144
147
|
isActive: user.isActive,
|
|
148
|
+
emailVerifiedAt: user.emailVerifiedAt ? user.emailVerifiedAt.toISOString() : null,
|
|
145
149
|
createdAt: user.createdAt.toISOString(),
|
|
146
150
|
updatedAt: user.updatedAt.toISOString(),
|
|
147
151
|
};
|
|
@@ -167,18 +171,21 @@ export async function register(
|
|
|
167
171
|
}
|
|
168
172
|
|
|
169
173
|
const hashedPassword = await hashPassword(input.password);
|
|
170
|
-
|
|
174
|
+
// A new account is never banned (isActive stays at its default true). When
|
|
175
|
+
// verification is required it starts unverified and gets no session until it
|
|
176
|
+
// proves its email; otherwise it counts as verified from the start.
|
|
177
|
+
const requiresVerification = env.REQUIRE_USER_VERIFICATION;
|
|
171
178
|
|
|
172
179
|
const user = await authRepo.createUser({
|
|
173
180
|
email: input.email,
|
|
174
181
|
password: hashedPassword,
|
|
175
182
|
firstName: input.firstName,
|
|
176
183
|
lastName: input.lastName,
|
|
177
|
-
|
|
184
|
+
emailVerifiedAt: requiresVerification ? null : new Date(),
|
|
178
185
|
});
|
|
179
186
|
|
|
180
187
|
// If verification is required, don't issue tokens — user must verify first
|
|
181
|
-
if (
|
|
188
|
+
if (requiresVerification) {
|
|
182
189
|
return {
|
|
183
190
|
user: sanitizeUser(user),
|
|
184
191
|
requiresVerification: true,
|
|
@@ -224,11 +231,6 @@ export async function login(
|
|
|
224
231
|
throw new UnauthorizedError('Invalid email or password', 'INVALID_CREDENTIALS');
|
|
225
232
|
}
|
|
226
233
|
|
|
227
|
-
// Distinct error for inactive accounts so the client can show proper messaging
|
|
228
|
-
if (!user.isActive) {
|
|
229
|
-
throw new ForbiddenError('Account is not activated. Please verify your account.', 'ACCOUNT_NOT_ACTIVE');
|
|
230
|
-
}
|
|
231
|
-
|
|
232
234
|
// Check account lockout — scoped to this email+IP pair (Redis), so an
|
|
233
235
|
// attacker spamming bad passwords only locks out their OWN address and
|
|
234
236
|
// cannot remotely lock the real user out (lockout DoS).
|
|
@@ -255,6 +257,10 @@ export async function login(
|
|
|
255
257
|
await authRepo.resetFailedAttempts(user.id);
|
|
256
258
|
}
|
|
257
259
|
|
|
260
|
+
// Reveal account state only now that the password is proven, so an anonymous
|
|
261
|
+
// caller cannot learn whether an email is banned or unverified.
|
|
262
|
+
assertAccountCanSignIn(user);
|
|
263
|
+
|
|
258
264
|
const accessToken = signAccessToken({
|
|
259
265
|
userId: user.id,
|
|
260
266
|
role: user.role,
|
|
@@ -294,13 +300,24 @@ export async function refresh(refreshToken: string): Promise<{ accessToken: stri
|
|
|
294
300
|
throw new UnauthorizedError('Refresh token expired', 'REFRESH_TOKEN_EXPIRED');
|
|
295
301
|
}
|
|
296
302
|
|
|
303
|
+
// A token is only as alive as its session. If the session row is gone
|
|
304
|
+
// (logout elsewhere, admin force-expire, cleanup job) or past its expiry, the
|
|
305
|
+
// login is over: drop the token instead of minting new ones.
|
|
306
|
+
const session = storedToken.sessionId
|
|
307
|
+
? await sessionRepository.getSessionById(storedToken.sessionId)
|
|
308
|
+
: null;
|
|
309
|
+
if (!session || session.expiresAt <= new Date()) {
|
|
310
|
+
await authRepo.deleteRefreshToken(refreshToken);
|
|
311
|
+
throw new UnauthorizedError('Session expired', 'SESSION_EXPIRED');
|
|
312
|
+
}
|
|
313
|
+
|
|
297
314
|
const user = await authRepo.findUserById(storedToken.userId);
|
|
298
315
|
if (!user) {
|
|
299
316
|
throw new UnauthorizedError('User not found or disabled', 'INVALID_REFRESH_TOKEN');
|
|
300
317
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
318
|
+
// A ban (or a pending verification) also stops refresh, so a banned user's
|
|
319
|
+
// leftover refresh token cannot mint new access tokens.
|
|
320
|
+
assertAccountCanSignIn(user);
|
|
304
321
|
|
|
305
322
|
const newAccessToken = signAccessToken({
|
|
306
323
|
userId: user.id,
|
|
@@ -313,7 +330,7 @@ export async function refresh(refreshToken: string): Promise<{ accessToken: stri
|
|
|
313
330
|
const rotated = await authRepo.rotateRefreshToken(refreshToken, {
|
|
314
331
|
token: newRefreshToken,
|
|
315
332
|
userId: user.id,
|
|
316
|
-
sessionId:
|
|
333
|
+
sessionId: session.id,
|
|
317
334
|
expiresAt: getRefreshTokenExpiresAt(),
|
|
318
335
|
});
|
|
319
336
|
|
|
@@ -38,18 +38,6 @@ export class SessionRepository {
|
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
/**
|
|
42
|
-
* Update session last active timestamp
|
|
43
|
-
*/
|
|
44
|
-
async updateLastActive(sessionId: string): Promise<Session> {
|
|
45
|
-
return prisma.session.update({
|
|
46
|
-
where: { id: sessionId },
|
|
47
|
-
data: {
|
|
48
|
-
lastActiveAt: new Date(),
|
|
49
|
-
},
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
41
|
/**
|
|
54
42
|
* Delete a specific session (no-op if already deleted)
|
|
55
43
|
*/
|
|
@@ -18,6 +18,7 @@ const userSelect = {
|
|
|
18
18
|
role: true,
|
|
19
19
|
avatarUrl: true,
|
|
20
20
|
isActive: true,
|
|
21
|
+
emailVerifiedAt: true,
|
|
21
22
|
createdAt: true,
|
|
22
23
|
updatedAt: true,
|
|
23
24
|
} as const;
|
|
@@ -30,6 +31,7 @@ export type SafeUser = {
|
|
|
30
31
|
role: UserRole;
|
|
31
32
|
avatarUrl: string | null;
|
|
32
33
|
isActive: boolean;
|
|
34
|
+
emailVerifiedAt: Date | null;
|
|
33
35
|
createdAt: Date;
|
|
34
36
|
updatedAt: Date;
|
|
35
37
|
};
|
|
@@ -28,12 +28,15 @@ export const testUsers = {
|
|
|
28
28
|
avatarUrl: null,
|
|
29
29
|
role: 'USER' as const,
|
|
30
30
|
isActive: true,
|
|
31
|
+
emailVerifiedAt: new Date('2024-01-01T00:00:00Z'),
|
|
31
32
|
deletedAt: null,
|
|
32
33
|
failedLoginAttempts: 0,
|
|
33
34
|
lockedUntil: null,
|
|
34
35
|
createdAt: new Date('2024-01-01T00:00:00Z'),
|
|
35
36
|
updatedAt: new Date('2024-01-01T00:00:00Z'),
|
|
36
37
|
},
|
|
38
|
+
// Banned by an admin (isActive=false). Its email was verified long ago:
|
|
39
|
+
// a ban is independent of email verification.
|
|
37
40
|
inactiveUser: {
|
|
38
41
|
id: 'test-user-id-2',
|
|
39
42
|
email: 'inactive@example.com',
|
|
@@ -43,6 +46,24 @@ export const testUsers = {
|
|
|
43
46
|
avatarUrl: null,
|
|
44
47
|
role: 'USER' as const,
|
|
45
48
|
isActive: false,
|
|
49
|
+
emailVerifiedAt: new Date('2024-01-01T00:00:00Z'),
|
|
50
|
+
deletedAt: null,
|
|
51
|
+
failedLoginAttempts: 0,
|
|
52
|
+
lockedUntil: null,
|
|
53
|
+
createdAt: new Date('2024-01-01T00:00:00Z'),
|
|
54
|
+
updatedAt: new Date('2024-01-01T00:00:00Z'),
|
|
55
|
+
},
|
|
56
|
+
// Registered while REQUIRE_USER_VERIFICATION was on and never verified.
|
|
57
|
+
unverifiedUser: {
|
|
58
|
+
id: 'test-user-id-4',
|
|
59
|
+
email: 'unverified@example.com',
|
|
60
|
+
password: '$2a$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYiIJU6u1Em',
|
|
61
|
+
firstName: 'Unverified',
|
|
62
|
+
lastName: 'User',
|
|
63
|
+
avatarUrl: null,
|
|
64
|
+
role: 'USER' as const,
|
|
65
|
+
isActive: true,
|
|
66
|
+
emailVerifiedAt: null,
|
|
46
67
|
deletedAt: null,
|
|
47
68
|
failedLoginAttempts: 0,
|
|
48
69
|
lockedUntil: null,
|
|
@@ -58,6 +79,7 @@ export const testUsers = {
|
|
|
58
79
|
avatarUrl: null,
|
|
59
80
|
role: 'ADMIN' as const,
|
|
60
81
|
isActive: true,
|
|
82
|
+
emailVerifiedAt: new Date('2024-01-01T00:00:00Z'),
|
|
61
83
|
deletedAt: null,
|
|
62
84
|
failedLoginAttempts: 0,
|
|
63
85
|
lockedUntil: null,
|