better-ts-stack 2.0.0 → 2.0.2
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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/args.js +2 -2
- package/dist/builder/buildPlanner.js +71 -0
- package/dist/builder/configGenerator.js +9 -7
- package/dist/builder/dependencyInstaller.js +0 -1
- package/dist/builder/fileProcessor.js +47 -44
- package/dist/builder/gitInitializer.js +20 -1
- package/dist/builder/index.js +19 -38
- package/dist/builder/moduleSelector.js +0 -1
- package/dist/builder/templateContext.js +0 -22
- package/dist/index.js +4 -3
- package/dist/modules/registry.js +4 -6
- package/dist/output/nextSteps.js +4 -3
- package/dist/prompts/index.js +0 -1
- package/dist/prompts/stack.js +4 -1
- package/dist/types/index.js +8 -4
- package/dist/ui/index.js +0 -1
- package/dist/ui/messages.js +25 -1
- package/dist/ui/prompts.js +0 -1
- package/dist/ui/theme.js +0 -1
- package/dist/validators/index.js +10 -41
- package/package.json +2 -1
- package/templates/backend/express/.prettierrc +2 -2
- package/templates/backend/express/config.json +1 -5
- package/templates/backend/express/eslint.config.mjs +40 -40
- package/templates/backend/express/gitignore +36 -36
- package/templates/backend/express/src/{index.ts → index.ts.hbs} +28 -29
- package/templates/backend/express/src/routes/health.ts +12 -12
- package/templates/backend/express/tsconfig.json +25 -25
- package/templates/backend/nestjs/.prettierrc +16 -16
- package/templates/backend/nestjs/config.json +1 -4
- package/templates/backend/nestjs/eslint.config.mjs +40 -40
- package/templates/backend/nestjs/gitignore +36 -36
- package/templates/backend/nestjs/nest-cli.json +9 -9
- package/templates/backend/nestjs/src/app.module.ts.hbs +34 -34
- package/templates/backend/nestjs/src/health/health.controller.ts +12 -12
- package/templates/backend/nestjs/src/main.ts +23 -23
- package/templates/backend/nestjs/tsconfig.json +25 -25
- package/templates/frontend/nextjs/components.json +19 -19
- package/templates/frontend/nextjs/config.json +1 -2
- package/templates/frontend/nextjs/gitignore +42 -42
- package/templates/frontend/nextjs/lib/utils.ts +6 -6
- package/templates/frontend/nextjs/proxy.ts.hbs +24 -24
- package/templates/frontend/nextjs/tsconfig.json +34 -34
- package/templates/frontend/tanstack/config.json +45 -48
- package/templates/frontend/tanstack/src/routeTree.gen.ts +68 -68
- package/templates/frontend/tanstack/src/styles/app.css +85 -85
- package/templates/modules/auth/express/config.json +1 -8
- package/templates/modules/auth/express/src/controllers/authController.ts.hbs +116 -116
- package/templates/modules/auth/express/src/middleware/requireAuth.ts.hbs +26 -26
- package/templates/modules/auth/express/src/services/userStore.ts.hbs +117 -117
- package/templates/modules/auth/nestjs/config.json +1 -5
- package/templates/modules/auth/nestjs/src/auth/auth.controller.ts +46 -46
- package/templates/modules/auth/nestjs/src/auth/auth.guard.ts +36 -36
- package/templates/modules/auth/nestjs/src/auth/auth.module.ts.hbs +22 -22
- package/templates/modules/auth/nestjs/src/auth/auth.schemas.ts +35 -35
- package/templates/modules/auth/nestjs/src/auth/auth.service.ts +70 -70
- package/templates/modules/auth/nestjs/src/auth/jwt.ts +27 -27
- package/templates/modules/auth/nestjs/src/auth/users.service.ts.hbs +144 -144
- package/templates/modules/auth/nestjs/src/auth/zod-validation.pipe.ts +16 -16
- package/templates/modules/auth/nextjs/app/dashboard/page.tsx +96 -96
- package/templates/modules/auth/nextjs/app/sign-in/page.tsx +35 -35
- package/templates/modules/auth/nextjs/app/sign-up/page.tsx +35 -35
- package/templates/modules/auth/nextjs/components/auth/sign-in-form.tsx +132 -132
- package/templates/modules/auth/nextjs/components/auth/sign-out-button.tsx +50 -50
- package/templates/modules/auth/nextjs/components/auth/sign-up-form.tsx +152 -152
- package/templates/modules/auth/nextjs/config.json +2 -15
- package/templates/modules/auth/nextjs/lib/auth-schema.ts +39 -39
- package/templates/modules/auth/nextjs/lib/auth.ts.hbs +37 -37
- package/templates/modules/auth/tanstack/config.json +2 -16
- package/templates/modules/auth/tanstack/src/lib/auth-functions.ts +4 -4
- package/templates/modules/auth/tanstack/src/lib/auth-schema.ts +38 -38
- package/templates/modules/auth/tanstack/src/routeTree.gen.ts +140 -140
- package/templates/modules/docker/.dockerignore +14 -14
- package/templates/modules/docker/Dockerfile.hbs +1 -3
- package/templates/modules/docker/config.json +1 -5
- package/templates/modules/docker/docker-compose.yml.hbs +9 -9
- package/templates/modules/drizzle/express/config.json +1 -2
- package/templates/modules/drizzle/express/drizzle.config.ts +13 -13
- package/templates/modules/drizzle/express/src/lib/db.ts +13 -13
- package/templates/modules/drizzle/express/src/lib/schema.ts +12 -10
- package/templates/modules/drizzle/nestjs/config.json +1 -2
- package/templates/modules/drizzle/nestjs/drizzle.config.ts +13 -13
- package/templates/modules/drizzle/nestjs/src/database/database.module.ts +30 -30
- package/templates/modules/drizzle/nestjs/src/database/schema.ts +12 -10
- package/templates/modules/drizzle/nextjs/config.json +1 -4
- package/templates/modules/drizzle/nextjs/lib/db.ts +13 -13
- package/templates/modules/drizzle/nextjs/lib/schema.ts.hbs +90 -84
- package/templates/modules/drizzle/tanstack/config.json +5 -5
- package/templates/modules/drizzle/tanstack/src/lib/schema.ts.hbs +90 -84
- package/templates/modules/mongoose/express/config.json +1 -2
- package/templates/modules/mongoose/nestjs/config.json +1 -2
- package/templates/modules/mongoose/nestjs/src/users/user.schema.ts +21 -21
- package/templates/modules/mongoose/nextjs/config.json +1 -2
- package/templates/modules/mongoose/nextjs/lib/db.ts +36 -36
- package/templates/modules/mongoose/tanstack/config.json +1 -2
- package/templates/modules/mongoose/tanstack/src/lib/db.ts +37 -37
- package/templates/modules/prisma/express/config.json +0 -1
- package/templates/modules/prisma/express/prisma/schema.prisma +1 -1
- package/templates/modules/prisma/express/prisma.config.ts +12 -12
- package/templates/modules/prisma/express/src/lib/prisma.ts +24 -24
- package/templates/modules/prisma/nestjs/config.json +0 -1
- package/templates/modules/prisma/nestjs/prisma/schema.prisma +23 -23
- package/templates/modules/prisma/nestjs/prisma.config.ts +12 -12
- package/templates/modules/prisma/nestjs/src/prisma/prisma.module.ts +11 -11
- package/templates/modules/prisma/nestjs/src/prisma/prisma.service.ts +17 -17
- package/templates/modules/prisma/nextjs/config.json +0 -1
- package/templates/modules/prisma/nextjs/prisma/schema.prisma.hbs +91 -91
- package/templates/modules/prisma/tanstack/config.json +0 -1
- package/dist/args.d.ts +0 -12
- package/dist/args.d.ts.map +0 -1
- package/dist/args.js.map +0 -1
- package/dist/builder/configGenerator.d.ts +0 -7
- package/dist/builder/configGenerator.d.ts.map +0 -1
- package/dist/builder/configGenerator.js.map +0 -1
- package/dist/builder/dependencyInstaller.d.ts +0 -3
- package/dist/builder/dependencyInstaller.d.ts.map +0 -1
- package/dist/builder/dependencyInstaller.js.map +0 -1
- package/dist/builder/fileProcessor.d.ts +0 -6
- package/dist/builder/fileProcessor.d.ts.map +0 -1
- package/dist/builder/fileProcessor.js.map +0 -1
- package/dist/builder/gitInitializer.d.ts +0 -2
- package/dist/builder/gitInitializer.d.ts.map +0 -1
- package/dist/builder/gitInitializer.js.map +0 -1
- package/dist/builder/index.d.ts +0 -3
- package/dist/builder/index.d.ts.map +0 -1
- package/dist/builder/index.js.map +0 -1
- package/dist/builder/moduleSelector.d.ts +0 -6
- package/dist/builder/moduleSelector.d.ts.map +0 -1
- package/dist/builder/moduleSelector.js.map +0 -1
- package/dist/builder/templateContext.d.ts +0 -6
- package/dist/builder/templateContext.d.ts.map +0 -1
- package/dist/builder/templateContext.js.map +0 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/modules/registry.d.ts +0 -6
- package/dist/modules/registry.d.ts.map +0 -1
- package/dist/modules/registry.js.map +0 -1
- package/dist/output/nextSteps.d.ts +0 -3
- package/dist/output/nextSteps.d.ts.map +0 -1
- package/dist/output/nextSteps.js.map +0 -1
- package/dist/prompts/index.d.ts +0 -4
- package/dist/prompts/index.d.ts.map +0 -1
- package/dist/prompts/index.js.map +0 -1
- package/dist/prompts/stack.d.ts +0 -4
- package/dist/prompts/stack.d.ts.map +0 -1
- package/dist/prompts/stack.js.map +0 -1
- package/dist/types/index.d.ts +0 -137
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js.map +0 -1
- package/dist/ui/index.d.ts +0 -3
- package/dist/ui/index.d.ts.map +0 -1
- package/dist/ui/index.js.map +0 -1
- package/dist/ui/messages.d.ts +0 -9
- package/dist/ui/messages.d.ts.map +0 -1
- package/dist/ui/messages.js.map +0 -1
- package/dist/ui/prompts.d.ts +0 -20
- package/dist/ui/prompts.d.ts.map +0 -1
- package/dist/ui/prompts.js.map +0 -1
- package/dist/ui/theme.d.ts +0 -9
- package/dist/ui/theme.d.ts.map +0 -1
- package/dist/ui/theme.js.map +0 -1
- package/dist/validators/index.d.ts +0 -6
- package/dist/validators/index.d.ts.map +0 -1
- package/dist/validators/index.js.map +0 -1
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ConflictException,
|
|
3
|
-
Injectable,
|
|
4
|
-
UnauthorizedException,
|
|
5
|
-
} from "@nestjs/common";
|
|
6
|
-
import bcrypt from "bcrypt";
|
|
7
|
-
|
|
8
|
-
import type { LoginInput, RegisterInput } from "./auth.schemas.js";
|
|
9
|
-
import { signToken } from "./jwt.js";
|
|
10
|
-
import {
|
|
11
|
-
type AuthUser,
|
|
12
|
-
isDuplicateEmailError,
|
|
13
|
-
UsersService,
|
|
14
|
-
} from "./users.service.js";
|
|
15
|
-
|
|
16
|
-
const SALT_ROUNDS = 10;
|
|
17
|
-
|
|
18
|
-
export interface AuthResponse {
|
|
19
|
-
user: Omit<AuthUser, "password">;
|
|
20
|
-
token: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
@Injectable()
|
|
24
|
-
export class AuthService {
|
|
25
|
-
constructor(private readonly usersService: UsersService) {}
|
|
26
|
-
|
|
27
|
-
async register({
|
|
28
|
-
email,
|
|
29
|
-
password,
|
|
30
|
-
name = null,
|
|
31
|
-
}: RegisterInput): Promise<AuthResponse> {
|
|
32
|
-
if (await this.usersService.findByEmail(email)) {
|
|
33
|
-
throw new ConflictException("User already exists");
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const hashedPassword = await bcrypt.hash(password, SALT_ROUNDS);
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
const user = await this.usersService.create({
|
|
40
|
-
email,
|
|
41
|
-
name,
|
|
42
|
-
password: hashedPassword,
|
|
43
|
-
});
|
|
44
|
-
return this.toAuthResponse(user);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
// A concurrent request registered the same email after the check above
|
|
47
|
-
if (isDuplicateEmailError(error)) {
|
|
48
|
-
throw new ConflictException("User already exists");
|
|
49
|
-
}
|
|
50
|
-
throw error;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async login({ email, password }: LoginInput): Promise<AuthResponse> {
|
|
55
|
-
const user = await this.usersService.findByEmail(email);
|
|
56
|
-
|
|
57
|
-
if (!user || !(await bcrypt.compare(password, user.password))) {
|
|
58
|
-
throw new UnauthorizedException("Invalid credentials");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return this.toAuthResponse(user);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
private toAuthResponse({ id, email, name }: AuthUser): AuthResponse {
|
|
65
|
-
return {
|
|
66
|
-
user: { id, email, name },
|
|
67
|
-
token: signToken({ sub: id, email, name }),
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ConflictException,
|
|
3
|
+
Injectable,
|
|
4
|
+
UnauthorizedException,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import bcrypt from "bcrypt";
|
|
7
|
+
|
|
8
|
+
import type { LoginInput, RegisterInput } from "./auth.schemas.js";
|
|
9
|
+
import { signToken } from "./jwt.js";
|
|
10
|
+
import {
|
|
11
|
+
type AuthUser,
|
|
12
|
+
isDuplicateEmailError,
|
|
13
|
+
UsersService,
|
|
14
|
+
} from "./users.service.js";
|
|
15
|
+
|
|
16
|
+
const SALT_ROUNDS = 10;
|
|
17
|
+
|
|
18
|
+
export interface AuthResponse {
|
|
19
|
+
user: Omit<AuthUser, "password">;
|
|
20
|
+
token: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Injectable()
|
|
24
|
+
export class AuthService {
|
|
25
|
+
constructor(private readonly usersService: UsersService) {}
|
|
26
|
+
|
|
27
|
+
async register({
|
|
28
|
+
email,
|
|
29
|
+
password,
|
|
30
|
+
name = null,
|
|
31
|
+
}: RegisterInput): Promise<AuthResponse> {
|
|
32
|
+
if (await this.usersService.findByEmail(email)) {
|
|
33
|
+
throw new ConflictException("User already exists");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const hashedPassword = await bcrypt.hash(password, SALT_ROUNDS);
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
const user = await this.usersService.create({
|
|
40
|
+
email,
|
|
41
|
+
name,
|
|
42
|
+
password: hashedPassword,
|
|
43
|
+
});
|
|
44
|
+
return this.toAuthResponse(user);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
// A concurrent request registered the same email after the check above
|
|
47
|
+
if (isDuplicateEmailError(error)) {
|
|
48
|
+
throw new ConflictException("User already exists");
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async login({ email, password }: LoginInput): Promise<AuthResponse> {
|
|
55
|
+
const user = await this.usersService.findByEmail(email);
|
|
56
|
+
|
|
57
|
+
if (!user || !(await bcrypt.compare(password, user.password))) {
|
|
58
|
+
throw new UnauthorizedException("Invalid credentials");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return this.toAuthResponse(user);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
private toAuthResponse({ id, email, name }: AuthUser): AuthResponse {
|
|
65
|
+
return {
|
|
66
|
+
user: { id, email, name },
|
|
67
|
+
token: signToken({ sub: id, email, name }),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import jwt, { type SignOptions } from "jsonwebtoken";
|
|
2
|
-
|
|
3
|
-
export interface AuthTokenPayload {
|
|
4
|
-
sub: string;
|
|
5
|
-
email: string;
|
|
6
|
-
name?: string | null;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const { JWT_SECRET, JWT_EXPIRES_IN } = process.env;
|
|
10
|
-
|
|
11
|
-
function ensureSecret(): string {
|
|
12
|
-
if (!JWT_SECRET) {
|
|
13
|
-
throw new Error("JWT_SECRET is not set");
|
|
14
|
-
}
|
|
15
|
-
return JWT_SECRET;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function signToken(payload: AuthTokenPayload): string {
|
|
19
|
-
const secret = ensureSecret();
|
|
20
|
-
const expiresIn = JWT_EXPIRES_IN || "1h";
|
|
21
|
-
return jwt.sign(payload, secret, { expiresIn } as SignOptions);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function verifyToken(token: string): AuthTokenPayload {
|
|
25
|
-
const secret = ensureSecret();
|
|
26
|
-
return jwt.verify(token, secret) as AuthTokenPayload;
|
|
27
|
-
}
|
|
1
|
+
import jwt, { type SignOptions } from "jsonwebtoken";
|
|
2
|
+
|
|
3
|
+
export interface AuthTokenPayload {
|
|
4
|
+
sub: string;
|
|
5
|
+
email: string;
|
|
6
|
+
name?: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const { JWT_SECRET, JWT_EXPIRES_IN } = process.env;
|
|
10
|
+
|
|
11
|
+
function ensureSecret(): string {
|
|
12
|
+
if (!JWT_SECRET) {
|
|
13
|
+
throw new Error("JWT_SECRET is not set");
|
|
14
|
+
}
|
|
15
|
+
return JWT_SECRET;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function signToken(payload: AuthTokenPayload): string {
|
|
19
|
+
const secret = ensureSecret();
|
|
20
|
+
const expiresIn = JWT_EXPIRES_IN || "1h";
|
|
21
|
+
return jwt.sign(payload, secret, { expiresIn } as SignOptions);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function verifyToken(token: string): AuthTokenPayload {
|
|
25
|
+
const secret = ensureSecret();
|
|
26
|
+
return jwt.verify(token, secret) as AuthTokenPayload;
|
|
27
|
+
}
|
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
{{#if (eq database 'drizzle')}}
|
|
2
|
-
import { Inject, Injectable } from "@nestjs/common";
|
|
3
|
-
import { eq } from "drizzle-orm";
|
|
4
|
-
|
|
5
|
-
import { type Database, DRIZZLE } from "../database/database.module.js";
|
|
6
|
-
import { users } from "../database/schema.js";
|
|
7
|
-
{{/if}}
|
|
8
|
-
{{#if (eq database 'prisma')}}
|
|
9
|
-
import { Injectable } from "@nestjs/common";
|
|
10
|
-
|
|
11
|
-
import { PrismaService } from "../prisma/prisma.service.js";
|
|
12
|
-
{{/if}}
|
|
13
|
-
{{#if (eq database 'mongoose')}}
|
|
14
|
-
import { Injectable } from "@nestjs/common";
|
|
15
|
-
import { InjectModel } from "@nestjs/mongoose";
|
|
16
|
-
import type { Model } from "mongoose";
|
|
17
|
-
|
|
18
|
-
import { User } from "../users/user.schema.js";
|
|
19
|
-
{{/if}}
|
|
20
|
-
{{#if (eq database 'none')}}
|
|
21
|
-
import { Injectable } from "@nestjs/common";
|
|
22
|
-
import { randomUUID } from "node:crypto";
|
|
23
|
-
{{/if}}
|
|
24
|
-
|
|
25
|
-
export interface AuthUser {
|
|
26
|
-
id: string;
|
|
27
|
-
email: string;
|
|
28
|
-
name?: string | null;
|
|
29
|
-
password: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface CreateUserInput {
|
|
33
|
-
email: string;
|
|
34
|
-
name?: string | null;
|
|
35
|
-
password: string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
@Injectable()
|
|
39
|
-
export class UsersService {
|
|
40
|
-
{{#if (eq database 'drizzle')}}
|
|
41
|
-
constructor(@Inject(DRIZZLE) private readonly db: Database) {}
|
|
42
|
-
|
|
43
|
-
async findByEmail(email: string): Promise<AuthUser | null> {
|
|
44
|
-
const [user] = await this.db
|
|
45
|
-
.select()
|
|
46
|
-
.from(users)
|
|
47
|
-
.where(eq(users.email, email))
|
|
48
|
-
.limit(1);
|
|
49
|
-
return user ?? null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async create(data: CreateUserInput): Promise<AuthUser> {
|
|
53
|
-
const [user] = await this.db.insert(users).values(data).returning();
|
|
54
|
-
return user;
|
|
55
|
-
}
|
|
56
|
-
{{/if}}
|
|
57
|
-
{{#if (eq database 'prisma')}}
|
|
58
|
-
constructor(private readonly prisma: PrismaService) {}
|
|
59
|
-
|
|
60
|
-
findByEmail(email: string): Promise<AuthUser | null> {
|
|
61
|
-
return this.prisma.user.findUnique({ where: { email } });
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
create(data: CreateUserInput): Promise<AuthUser> {
|
|
65
|
-
return this.prisma.user.create({ data });
|
|
66
|
-
}
|
|
67
|
-
{{/if}}
|
|
68
|
-
{{#if (eq database 'mongoose')}}
|
|
69
|
-
constructor(
|
|
70
|
-
@InjectModel(User.name) private readonly userModel: Model<User>
|
|
71
|
-
) {}
|
|
72
|
-
|
|
73
|
-
async findByEmail(email: string): Promise<AuthUser | null> {
|
|
74
|
-
const user = await this.userModel.findOne({ email }).exec();
|
|
75
|
-
if (!user) return null;
|
|
76
|
-
return {
|
|
77
|
-
id: user._id.toString(),
|
|
78
|
-
email: user.email,
|
|
79
|
-
name: user.name,
|
|
80
|
-
password: user.password,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
async create(data: CreateUserInput): Promise<AuthUser> {
|
|
85
|
-
const user = await this.userModel.create(data);
|
|
86
|
-
return {
|
|
87
|
-
id: user._id.toString(),
|
|
88
|
-
email: user.email,
|
|
89
|
-
name: user.name,
|
|
90
|
-
password: user.password,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
{{/if}}
|
|
94
|
-
{{#if (eq database 'none')}}
|
|
95
|
-
// In-memory store for trying the API out: users are lost on restart, so add
|
|
96
|
-
// a database before going to production
|
|
97
|
-
private readonly users: AuthUser[] = [];
|
|
98
|
-
|
|
99
|
-
findByEmail(email: string): Promise<AuthUser | null> {
|
|
100
|
-
const user = this.users.find((candidate) => candidate.email === email);
|
|
101
|
-
return Promise.resolve(user ?? null);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
create(data: CreateUserInput): Promise<AuthUser> {
|
|
105
|
-
// Enforce unique emails like a database constraint would, so a sign-up that
|
|
106
|
-
// raced past the findByEmail check is rejected instead of duplicated
|
|
107
|
-
if (this.users.some((candidate) => candidate.email === data.email)) {
|
|
108
|
-
return Promise.reject(
|
|
109
|
-
Object.assign(new Error("User already exists"), {
|
|
110
|
-
code: "DUPLICATE_EMAIL",
|
|
111
|
-
})
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const user: AuthUser = { id: randomUUID(), ...data };
|
|
116
|
-
this.users.push(user);
|
|
117
|
-
return Promise.resolve(user);
|
|
118
|
-
}
|
|
119
|
-
{{/if}}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Unique-constraint error codes: Prisma, PostgreSQL (Drizzle wraps it in `cause`),
|
|
123
|
-
// MongoDB, and the in-memory store
|
|
124
|
-
const DUPLICATE_KEY_CODES = new Set<unknown>([
|
|
125
|
-
"P2002",
|
|
126
|
-
"23505",
|
|
127
|
-
11000,
|
|
128
|
-
"DUPLICATE_EMAIL",
|
|
129
|
-
]);
|
|
130
|
-
|
|
131
|
-
// True when an insert failed because the email is already taken (e.g. concurrent sign-ups)
|
|
132
|
-
export function isDuplicateEmailError(error: unknown): boolean {
|
|
133
|
-
let current = error;
|
|
134
|
-
for (
|
|
135
|
-
let depth = 0;
|
|
136
|
-
depth < 5 && current && typeof current === "object";
|
|
137
|
-
depth++
|
|
138
|
-
) {
|
|
139
|
-
if (DUPLICATE_KEY_CODES.has((current as { code?: unknown }).code))
|
|
140
|
-
return true;
|
|
141
|
-
current = (current as { cause?: unknown }).cause;
|
|
142
|
-
}
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
1
|
+
{{#if (eq database 'drizzle')}}
|
|
2
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
3
|
+
import { eq } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import { type Database, DRIZZLE } from "../database/database.module.js";
|
|
6
|
+
import { users } from "../database/schema.js";
|
|
7
|
+
{{/if}}
|
|
8
|
+
{{#if (eq database 'prisma')}}
|
|
9
|
+
import { Injectable } from "@nestjs/common";
|
|
10
|
+
|
|
11
|
+
import { PrismaService } from "../prisma/prisma.service.js";
|
|
12
|
+
{{/if}}
|
|
13
|
+
{{#if (eq database 'mongoose')}}
|
|
14
|
+
import { Injectable } from "@nestjs/common";
|
|
15
|
+
import { InjectModel } from "@nestjs/mongoose";
|
|
16
|
+
import type { Model } from "mongoose";
|
|
17
|
+
|
|
18
|
+
import { User } from "../users/user.schema.js";
|
|
19
|
+
{{/if}}
|
|
20
|
+
{{#if (eq database 'none')}}
|
|
21
|
+
import { Injectable } from "@nestjs/common";
|
|
22
|
+
import { randomUUID } from "node:crypto";
|
|
23
|
+
{{/if}}
|
|
24
|
+
|
|
25
|
+
export interface AuthUser {
|
|
26
|
+
id: string;
|
|
27
|
+
email: string;
|
|
28
|
+
name?: string | null;
|
|
29
|
+
password: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CreateUserInput {
|
|
33
|
+
email: string;
|
|
34
|
+
name?: string | null;
|
|
35
|
+
password: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Injectable()
|
|
39
|
+
export class UsersService {
|
|
40
|
+
{{#if (eq database 'drizzle')}}
|
|
41
|
+
constructor(@Inject(DRIZZLE) private readonly db: Database) {}
|
|
42
|
+
|
|
43
|
+
async findByEmail(email: string): Promise<AuthUser | null> {
|
|
44
|
+
const [user] = await this.db
|
|
45
|
+
.select()
|
|
46
|
+
.from(users)
|
|
47
|
+
.where(eq(users.email, email))
|
|
48
|
+
.limit(1);
|
|
49
|
+
return user ?? null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async create(data: CreateUserInput): Promise<AuthUser> {
|
|
53
|
+
const [user] = await this.db.insert(users).values(data).returning();
|
|
54
|
+
return user;
|
|
55
|
+
}
|
|
56
|
+
{{/if}}
|
|
57
|
+
{{#if (eq database 'prisma')}}
|
|
58
|
+
constructor(private readonly prisma: PrismaService) {}
|
|
59
|
+
|
|
60
|
+
findByEmail(email: string): Promise<AuthUser | null> {
|
|
61
|
+
return this.prisma.user.findUnique({ where: { email } });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
create(data: CreateUserInput): Promise<AuthUser> {
|
|
65
|
+
return this.prisma.user.create({ data });
|
|
66
|
+
}
|
|
67
|
+
{{/if}}
|
|
68
|
+
{{#if (eq database 'mongoose')}}
|
|
69
|
+
constructor(
|
|
70
|
+
@InjectModel(User.name) private readonly userModel: Model<User>
|
|
71
|
+
) {}
|
|
72
|
+
|
|
73
|
+
async findByEmail(email: string): Promise<AuthUser | null> {
|
|
74
|
+
const user = await this.userModel.findOne({ email }).exec();
|
|
75
|
+
if (!user) return null;
|
|
76
|
+
return {
|
|
77
|
+
id: user._id.toString(),
|
|
78
|
+
email: user.email,
|
|
79
|
+
name: user.name,
|
|
80
|
+
password: user.password,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async create(data: CreateUserInput): Promise<AuthUser> {
|
|
85
|
+
const user = await this.userModel.create(data);
|
|
86
|
+
return {
|
|
87
|
+
id: user._id.toString(),
|
|
88
|
+
email: user.email,
|
|
89
|
+
name: user.name,
|
|
90
|
+
password: user.password,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
{{/if}}
|
|
94
|
+
{{#if (eq database 'none')}}
|
|
95
|
+
// In-memory store for trying the API out: users are lost on restart, so add
|
|
96
|
+
// a database before going to production
|
|
97
|
+
private readonly users: AuthUser[] = [];
|
|
98
|
+
|
|
99
|
+
findByEmail(email: string): Promise<AuthUser | null> {
|
|
100
|
+
const user = this.users.find((candidate) => candidate.email === email);
|
|
101
|
+
return Promise.resolve(user ?? null);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
create(data: CreateUserInput): Promise<AuthUser> {
|
|
105
|
+
// Enforce unique emails like a database constraint would, so a sign-up that
|
|
106
|
+
// raced past the findByEmail check is rejected instead of duplicated
|
|
107
|
+
if (this.users.some((candidate) => candidate.email === data.email)) {
|
|
108
|
+
return Promise.reject(
|
|
109
|
+
Object.assign(new Error("User already exists"), {
|
|
110
|
+
code: "DUPLICATE_EMAIL",
|
|
111
|
+
})
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const user: AuthUser = { id: randomUUID(), ...data };
|
|
116
|
+
this.users.push(user);
|
|
117
|
+
return Promise.resolve(user);
|
|
118
|
+
}
|
|
119
|
+
{{/if}}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Unique-constraint error codes: Prisma, PostgreSQL (Drizzle wraps it in `cause`),
|
|
123
|
+
// MongoDB, and the in-memory store
|
|
124
|
+
const DUPLICATE_KEY_CODES = new Set<unknown>([
|
|
125
|
+
"P2002",
|
|
126
|
+
"23505",
|
|
127
|
+
11000,
|
|
128
|
+
"DUPLICATE_EMAIL",
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
// True when an insert failed because the email is already taken (e.g. concurrent sign-ups)
|
|
132
|
+
export function isDuplicateEmailError(error: unknown): boolean {
|
|
133
|
+
let current = error;
|
|
134
|
+
for (
|
|
135
|
+
let depth = 0;
|
|
136
|
+
depth < 5 && current && typeof current === "object";
|
|
137
|
+
depth++
|
|
138
|
+
) {
|
|
139
|
+
if (DUPLICATE_KEY_CODES.has((current as { code?: unknown }).code))
|
|
140
|
+
return true;
|
|
141
|
+
current = (current as { cause?: unknown }).cause;
|
|
142
|
+
}
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { BadRequestException, type PipeTransform } from "@nestjs/common";
|
|
2
|
-
import type { ZodType } from "zod";
|
|
3
|
-
|
|
4
|
-
// Validates a request body against a Zod schema and returns the parsed value
|
|
5
|
-
export class ZodValidationPipe<T> implements PipeTransform<unknown, T> {
|
|
6
|
-
constructor(private readonly schema: ZodType<T>) {}
|
|
7
|
-
|
|
8
|
-
transform(value: unknown): T {
|
|
9
|
-
// The body is undefined when the request has none
|
|
10
|
-
const result = this.schema.safeParse(value ?? {});
|
|
11
|
-
if (!result.success) {
|
|
12
|
-
throw new BadRequestException(result.error.issues[0].message);
|
|
13
|
-
}
|
|
14
|
-
return result.data;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
1
|
+
import { BadRequestException, type PipeTransform } from "@nestjs/common";
|
|
2
|
+
import type { ZodType } from "zod";
|
|
3
|
+
|
|
4
|
+
// Validates a request body against a Zod schema and returns the parsed value
|
|
5
|
+
export class ZodValidationPipe<T> implements PipeTransform<unknown, T> {
|
|
6
|
+
constructor(private readonly schema: ZodType<T>) {}
|
|
7
|
+
|
|
8
|
+
transform(value: unknown): T {
|
|
9
|
+
// The body is undefined when the request has none
|
|
10
|
+
const result = this.schema.safeParse(value ?? {});
|
|
11
|
+
if (!result.success) {
|
|
12
|
+
throw new BadRequestException(result.error.issues[0].message);
|
|
13
|
+
}
|
|
14
|
+
return result.data;
|
|
15
|
+
}
|
|
16
|
+
}
|