nestforge-generator 0.1.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/LICENSE +21 -0
- package/README.md +371 -0
- package/README.pt-BR.md +370 -0
- package/dist/features/auth-strategy.js +19 -0
- package/dist/features/auth-strategy.js.map +1 -0
- package/dist/features/database.js +59 -0
- package/dist/features/database.js.map +1 -0
- package/dist/features/dependencies.js +57 -0
- package/dist/features/dependencies.js.map +1 -0
- package/dist/features/language.js +172 -0
- package/dist/features/language.js.map +1 -0
- package/dist/features/markers.js +116 -0
- package/dist/features/markers.js.map +1 -0
- package/dist/generator.js +120 -0
- package/dist/generator.js.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts.js +137 -0
- package/dist/prompts.js.map +1 -0
- package/package.json +76 -0
- package/templates/drizzle/.env.example +45 -0
- package/templates/drizzle/.env.test +38 -0
- package/templates/drizzle/.github/workflows/ci.yml +119 -0
- package/templates/drizzle/ARCHITECTURE.md +227 -0
- package/templates/drizzle/ARCHITECTURE.pt-BR.md +225 -0
- package/templates/drizzle/CODE_OF_CONDUCT.md +29 -0
- package/templates/drizzle/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/drizzle/CONTRIBUTING.md +57 -0
- package/templates/drizzle/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/drizzle/Dockerfile +43 -0
- package/templates/drizzle/LICENSE +21 -0
- package/templates/drizzle/README.md +257 -0
- package/templates/drizzle/README.pt-BR.md +257 -0
- package/templates/drizzle/ROADMAP.md +143 -0
- package/templates/drizzle/ROADMAP.pt-BR.md +143 -0
- package/templates/drizzle/TESTING.md +136 -0
- package/templates/drizzle/TESTING.pt-BR.md +136 -0
- package/templates/drizzle/docker-compose.yml +82 -0
- package/templates/drizzle/docs/adding-a-module.md +585 -0
- package/templates/drizzle/docs/features-markers.md +457 -0
- package/templates/drizzle/drizzle.config.ts +49 -0
- package/templates/drizzle/nest-cli.json +8 -0
- package/templates/drizzle/package.json +96 -0
- package/templates/drizzle/src/app.module.ts +72 -0
- package/templates/drizzle/src/auth/auth.controller.ts +283 -0
- package/templates/drizzle/src/auth/auth.module.ts +81 -0
- package/templates/drizzle/src/auth/auth.service.spec.ts +111 -0
- package/templates/drizzle/src/auth/auth.service.ts +631 -0
- package/templates/drizzle/src/auth/drizzle-session.store.ts +263 -0
- package/templates/drizzle/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/drizzle/src/auth/dto/login.dto.ts +10 -0
- package/templates/drizzle/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/drizzle/src/auth/dto/register.dto.ts +11 -0
- package/templates/drizzle/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/drizzle/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/drizzle/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/drizzle/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/drizzle/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/drizzle/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/drizzle/src/auth/session.service.spec.ts +74 -0
- package/templates/drizzle/src/auth/session.service.ts +96 -0
- package/templates/drizzle/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/drizzle/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/drizzle/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/drizzle/src/auth/token.service.ts +175 -0
- package/templates/drizzle/src/common/constants/permissions.ts +8 -0
- package/templates/drizzle/src/common/constants/role-permissions.ts +9 -0
- package/templates/drizzle/src/common/constants/role.enum.ts +5 -0
- package/templates/drizzle/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/drizzle/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/drizzle/src/common/decorators/public.decorator.ts +4 -0
- package/templates/drizzle/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/drizzle/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/drizzle/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/drizzle/src/common/guards/permissions.guard.ts +28 -0
- package/templates/drizzle/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/drizzle/src/common/guards/roles.guard.ts +24 -0
- package/templates/drizzle/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/drizzle/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/drizzle/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/drizzle/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/drizzle/src/common/utils/hash.util.ts +5 -0
- package/templates/drizzle/src/config/env.validation.ts +50 -0
- package/templates/drizzle/src/database/database-lifecycle.service.ts +30 -0
- package/templates/drizzle/src/database/database.constants.ts +2 -0
- package/templates/drizzle/src/database/database.decorators.ts +5 -0
- package/templates/drizzle/src/database/database.module.ts +96 -0
- package/templates/drizzle/src/database/database.types.ts +28 -0
- package/templates/drizzle/src/database/schema/index.ts +11 -0
- package/templates/drizzle/src/database/schema/mysql.schema.ts +215 -0
- package/templates/drizzle/src/database/schema/postgres.schema.ts +218 -0
- package/templates/drizzle/src/database/schema/sqlite.schema.ts +190 -0
- package/templates/drizzle/src/database/seed.ts +152 -0
- package/templates/drizzle/src/health/health.controller.ts +49 -0
- package/templates/drizzle/src/health/health.module.ts +19 -0
- package/templates/drizzle/src/health/indicators/drizzle-health.indicator.spec.ts +71 -0
- package/templates/drizzle/src/health/indicators/drizzle-health.indicator.ts +52 -0
- package/templates/drizzle/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/drizzle/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/drizzle/src/mail/mail.module.ts +12 -0
- package/templates/drizzle/src/mail/mail.processor.ts +48 -0
- package/templates/drizzle/src/mail/mail.service.ts +24 -0
- package/templates/drizzle/src/mail/templates/email-templates.ts +28 -0
- package/templates/drizzle/src/main.ts +145 -0
- package/templates/drizzle/src/metrics/metrics.controller.ts +22 -0
- package/templates/drizzle/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/drizzle/src/metrics/metrics.module.ts +12 -0
- package/templates/drizzle/src/metrics/metrics.service.ts +34 -0
- package/templates/drizzle/src/types/express-session.d.ts +13 -0
- package/templates/drizzle/src/users/dto/create-user.dto.ts +12 -0
- package/templates/drizzle/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/drizzle/src/users/dto/update-user.dto.ts +6 -0
- package/templates/drizzle/src/users/entities/user.entity.ts +23 -0
- package/templates/drizzle/src/users/users.controller.ts +165 -0
- package/templates/drizzle/src/users/users.module.ts +10 -0
- package/templates/drizzle/src/users/users.service.spec.ts +301 -0
- package/templates/drizzle/src/users/users.service.ts +213 -0
- package/templates/drizzle/test/auth.e2e-spec.ts +114 -0
- package/templates/drizzle/test/session-auth.e2e-spec.ts +163 -0
- package/templates/drizzle/test/users.e2e-spec.ts +212 -0
- package/templates/drizzle/test/utils/clean-database.ts +104 -0
- package/templates/drizzle/test/utils/e2e-setup.ts +77 -0
- package/templates/drizzle/tsconfig.build.json +4 -0
- package/templates/drizzle/tsconfig.json +25 -0
- package/templates/drizzle/vitest.config.ts +20 -0
- package/templates/drizzle/vitest.e2e.config.ts +19 -0
- package/templates/prisma/.env.example +44 -0
- package/templates/prisma/.env.test +37 -0
- package/templates/prisma/.github/workflows/ci.yml +109 -0
- package/templates/prisma/ARCHITECTURE.md +70 -0
- package/templates/prisma/ARCHITECTURE.pt-BR.md +63 -0
- package/templates/prisma/CODE_OF_CONDUCT.md +29 -0
- package/templates/prisma/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/prisma/CONTRIBUTING.md +57 -0
- package/templates/prisma/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/prisma/Dockerfile +33 -0
- package/templates/prisma/LICENSE +21 -0
- package/templates/prisma/README.md +267 -0
- package/templates/prisma/README.pt-BR.md +267 -0
- package/templates/prisma/ROADMAP.md +75 -0
- package/templates/prisma/ROADMAP.pt-BR.md +65 -0
- package/templates/prisma/TESTING.md +123 -0
- package/templates/prisma/TESTING.pt-BR.md +123 -0
- package/templates/prisma/docker-compose.yml +78 -0
- package/templates/prisma/docs/adding-a-module.md +230 -0
- package/templates/prisma/docs/features-markers.md +50 -0
- package/templates/prisma/nest-cli.json +8 -0
- package/templates/prisma/package.json +95 -0
- package/templates/prisma/prisma/schema.prisma +105 -0
- package/templates/prisma/prisma/seed.ts +46 -0
- package/templates/prisma/src/app.module.ts +72 -0
- package/templates/prisma/src/auth/auth.controller.ts +283 -0
- package/templates/prisma/src/auth/auth.module.ts +59 -0
- package/templates/prisma/src/auth/auth.service.spec.ts +53 -0
- package/templates/prisma/src/auth/auth.service.ts +224 -0
- package/templates/prisma/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/prisma/src/auth/dto/login.dto.ts +10 -0
- package/templates/prisma/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/prisma/src/auth/dto/register.dto.ts +11 -0
- package/templates/prisma/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/prisma/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/prisma/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/prisma/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/prisma/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/prisma/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/prisma/src/auth/session.service.spec.ts +74 -0
- package/templates/prisma/src/auth/session.service.ts +96 -0
- package/templates/prisma/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/prisma/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/prisma/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/prisma/src/auth/token.service.ts +84 -0
- package/templates/prisma/src/common/constants/permissions.ts +8 -0
- package/templates/prisma/src/common/constants/role-permissions.ts +9 -0
- package/templates/prisma/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/prisma/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/prisma/src/common/decorators/public.decorator.ts +4 -0
- package/templates/prisma/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/prisma/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/prisma/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/prisma/src/common/guards/permissions.guard.ts +28 -0
- package/templates/prisma/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/prisma/src/common/guards/roles.guard.ts +24 -0
- package/templates/prisma/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/prisma/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/prisma/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/prisma/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/prisma/src/common/utils/hash.util.ts +5 -0
- package/templates/prisma/src/config/env.validation.ts +49 -0
- package/templates/prisma/src/database/prisma.module.ts +9 -0
- package/templates/prisma/src/database/prisma.service.ts +13 -0
- package/templates/prisma/src/health/health.controller.ts +49 -0
- package/templates/prisma/src/health/health.module.ts +19 -0
- package/templates/prisma/src/health/indicators/prisma-health.indicator.spec.ts +22 -0
- package/templates/prisma/src/health/indicators/prisma-health.indicator.ts +19 -0
- package/templates/prisma/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/prisma/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/prisma/src/mail/mail.module.ts +12 -0
- package/templates/prisma/src/mail/mail.processor.ts +48 -0
- package/templates/prisma/src/mail/mail.service.ts +24 -0
- package/templates/prisma/src/mail/templates/email-templates.ts +28 -0
- package/templates/prisma/src/main.ts +92 -0
- package/templates/prisma/src/metrics/metrics.controller.ts +22 -0
- package/templates/prisma/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/prisma/src/metrics/metrics.module.ts +12 -0
- package/templates/prisma/src/metrics/metrics.service.ts +34 -0
- package/templates/prisma/src/types/express-session.d.ts +13 -0
- package/templates/prisma/src/users/dto/create-user.dto.ts +12 -0
- package/templates/prisma/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/prisma/src/users/dto/update-user.dto.ts +6 -0
- package/templates/prisma/src/users/entities/user.entity.ts +20 -0
- package/templates/prisma/src/users/users.controller.ts +165 -0
- package/templates/prisma/src/users/users.module.ts +10 -0
- package/templates/prisma/src/users/users.service.spec.ts +104 -0
- package/templates/prisma/src/users/users.service.ts +114 -0
- package/templates/prisma/test/auth.e2e-spec.ts +75 -0
- package/templates/prisma/test/session-auth.e2e-spec.ts +163 -0
- package/templates/prisma/test/users.e2e-spec.ts +106 -0
- package/templates/prisma/test/utils/clean-database.ts +14 -0
- package/templates/prisma/test/utils/e2e-setup.ts +58 -0
- package/templates/prisma/tsconfig.build.json +4 -0
- package/templates/prisma/tsconfig.json +25 -0
- package/templates/prisma/vitest.config.ts +20 -0
- package/templates/prisma/vitest.e2e.config.ts +19 -0
- package/templates/typeorm/.env.example +45 -0
- package/templates/typeorm/.env.test +38 -0
- package/templates/typeorm/.github/workflows/ci.yml +116 -0
- package/templates/typeorm/ARCHITECTURE.md +158 -0
- package/templates/typeorm/ARCHITECTURE.pt-BR.md +156 -0
- package/templates/typeorm/CODE_OF_CONDUCT.md +29 -0
- package/templates/typeorm/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/typeorm/CONTRIBUTING.md +57 -0
- package/templates/typeorm/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/typeorm/Dockerfile +43 -0
- package/templates/typeorm/LICENSE +21 -0
- package/templates/typeorm/README.md +266 -0
- package/templates/typeorm/README.pt-BR.md +266 -0
- package/templates/typeorm/ROADMAP.md +79 -0
- package/templates/typeorm/ROADMAP.pt-BR.md +67 -0
- package/templates/typeorm/TESTING.md +114 -0
- package/templates/typeorm/TESTING.pt-BR.md +114 -0
- package/templates/typeorm/docker-compose.yml +78 -0
- package/templates/typeorm/docs/adding-a-module.md +369 -0
- package/templates/typeorm/docs/features-markers.md +50 -0
- package/templates/typeorm/nest-cli.json +8 -0
- package/templates/typeorm/package.json +98 -0
- package/templates/typeorm/src/app.module.ts +72 -0
- package/templates/typeorm/src/auth/auth.controller.ts +283 -0
- package/templates/typeorm/src/auth/auth.module.ts +86 -0
- package/templates/typeorm/src/auth/auth.service.spec.ts +111 -0
- package/templates/typeorm/src/auth/auth.service.ts +346 -0
- package/templates/typeorm/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/typeorm/src/auth/dto/login.dto.ts +10 -0
- package/templates/typeorm/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/typeorm/src/auth/dto/register.dto.ts +11 -0
- package/templates/typeorm/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/typeorm/src/auth/entities/email-verification-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/oauth-account.entity.ts +50 -0
- package/templates/typeorm/src/auth/entities/password-reset-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/refresh-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/session.entity.ts +50 -0
- package/templates/typeorm/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/typeorm/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/typeorm/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/typeorm/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/typeorm/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/typeorm/src/auth/session.service.spec.ts +74 -0
- package/templates/typeorm/src/auth/session.service.ts +96 -0
- package/templates/typeorm/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/typeorm/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/typeorm/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/typeorm/src/auth/token.service.ts +115 -0
- package/templates/typeorm/src/common/constants/permissions.ts +8 -0
- package/templates/typeorm/src/common/constants/role-permissions.ts +9 -0
- package/templates/typeorm/src/common/constants/role.enum.ts +5 -0
- package/templates/typeorm/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/typeorm/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/typeorm/src/common/decorators/public.decorator.ts +4 -0
- package/templates/typeorm/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/typeorm/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/typeorm/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/typeorm/src/common/guards/permissions.guard.ts +28 -0
- package/templates/typeorm/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/typeorm/src/common/guards/roles.guard.ts +24 -0
- package/templates/typeorm/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/typeorm/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/typeorm/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/typeorm/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/typeorm/src/common/utils/hash.util.ts +5 -0
- package/templates/typeorm/src/config/env.validation.ts +50 -0
- package/templates/typeorm/src/database/data-source.ts +20 -0
- package/templates/typeorm/src/database/database.module.ts +39 -0
- package/templates/typeorm/src/database/seed.ts +81 -0
- package/templates/typeorm/src/database/typeorm-options.ts +49 -0
- package/templates/typeorm/src/health/health.controller.ts +49 -0
- package/templates/typeorm/src/health/health.module.ts +19 -0
- package/templates/typeorm/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/typeorm/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/typeorm/src/health/indicators/typeorm-health.indicator.spec.ts +49 -0
- package/templates/typeorm/src/health/indicators/typeorm-health.indicator.ts +37 -0
- package/templates/typeorm/src/mail/mail.module.ts +12 -0
- package/templates/typeorm/src/mail/mail.processor.ts +48 -0
- package/templates/typeorm/src/mail/mail.service.ts +24 -0
- package/templates/typeorm/src/mail/templates/email-templates.ts +28 -0
- package/templates/typeorm/src/main.ts +107 -0
- package/templates/typeorm/src/metrics/metrics.controller.ts +22 -0
- package/templates/typeorm/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/typeorm/src/metrics/metrics.module.ts +12 -0
- package/templates/typeorm/src/metrics/metrics.service.ts +34 -0
- package/templates/typeorm/src/types/express-session.d.ts +13 -0
- package/templates/typeorm/src/users/dto/create-user.dto.ts +12 -0
- package/templates/typeorm/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/typeorm/src/users/dto/update-user.dto.ts +6 -0
- package/templates/typeorm/src/users/entities/user.entity.ts +113 -0
- package/templates/typeorm/src/users/users.controller.ts +165 -0
- package/templates/typeorm/src/users/users.module.ts +13 -0
- package/templates/typeorm/src/users/users.service.spec.ts +183 -0
- package/templates/typeorm/src/users/users.service.ts +124 -0
- package/templates/typeorm/test/auth.e2e-spec.ts +111 -0
- package/templates/typeorm/test/session-auth.e2e-spec.ts +159 -0
- package/templates/typeorm/test/users.e2e-spec.ts +183 -0
- package/templates/typeorm/test/utils/clean-database.ts +65 -0
- package/templates/typeorm/test/utils/e2e-setup.ts +77 -0
- package/templates/typeorm/tsconfig.build.json +4 -0
- package/templates/typeorm/tsconfig.json +25 -0
- package/templates/typeorm/vitest.config.ts +20 -0
- package/templates/typeorm/vitest.e2e.config.ts +19 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConflictException,
|
|
3
|
+
Injectable,
|
|
4
|
+
UnauthorizedException,
|
|
5
|
+
} from '@nestjs/common';
|
|
6
|
+
import {
|
|
7
|
+
and,
|
|
8
|
+
eq,
|
|
9
|
+
isNull,
|
|
10
|
+
} from 'drizzle-orm';
|
|
11
|
+
import * as bcrypt from 'bcryptjs';
|
|
12
|
+
import {
|
|
13
|
+
createHash,
|
|
14
|
+
randomBytes,
|
|
15
|
+
randomUUID,
|
|
16
|
+
} from 'node:crypto';
|
|
17
|
+
import { Role } from '../common/constants/role.enum';
|
|
18
|
+
import { InjectDatabase } from '../database/database.decorators';
|
|
19
|
+
import type { DrizzleDatabase } from '../database/database.types';
|
|
20
|
+
import {
|
|
21
|
+
oauthAccounts,
|
|
22
|
+
users,
|
|
23
|
+
} from '../database/schema';
|
|
24
|
+
|
|
25
|
+
// nestforge:feature:auth:token
|
|
26
|
+
import { refreshTokens } from '../database/schema';
|
|
27
|
+
// nestforge:feature:auth:token:end
|
|
28
|
+
|
|
29
|
+
// nestforge:feature:redis,auth:password
|
|
30
|
+
import { MailService } from '../mail/mail.service';
|
|
31
|
+
import {
|
|
32
|
+
emailVerificationTokens,
|
|
33
|
+
passwordResetTokens,
|
|
34
|
+
} from '../database/schema';
|
|
35
|
+
// nestforge:feature:redis,auth:password:end
|
|
36
|
+
|
|
37
|
+
// nestforge:feature:auth:password
|
|
38
|
+
import { RegisterDto } from './dto/register.dto';
|
|
39
|
+
import { LoginDto } from './dto/login.dto';
|
|
40
|
+
// nestforge:feature:auth:password:end
|
|
41
|
+
|
|
42
|
+
import { OAuthProfile } from './strategies/google.strategy';
|
|
43
|
+
|
|
44
|
+
@Injectable()
|
|
45
|
+
export class AuthService {
|
|
46
|
+
constructor(
|
|
47
|
+
@InjectDatabase()
|
|
48
|
+
private readonly database: DrizzleDatabase,
|
|
49
|
+
|
|
50
|
+
// nestforge:feature:redis,auth:password
|
|
51
|
+
private readonly mailService: MailService,
|
|
52
|
+
// nestforge:feature:redis,auth:password:end
|
|
53
|
+
) { }
|
|
54
|
+
|
|
55
|
+
// nestforge:feature:auth:password
|
|
56
|
+
async register(dto: RegisterDto) {
|
|
57
|
+
const existing = await this.findUserByEmail(
|
|
58
|
+
dto.email,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (existing) {
|
|
62
|
+
throw new ConflictException(
|
|
63
|
+
'E-mail já cadastrado',
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const id = randomUUID();
|
|
68
|
+
const passwordHash = await bcrypt.hash(
|
|
69
|
+
dto.password,
|
|
70
|
+
10,
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
await this.database.insert(users).values({
|
|
74
|
+
id,
|
|
75
|
+
name: dto.name,
|
|
76
|
+
email: dto.email,
|
|
77
|
+
passwordHash,
|
|
78
|
+
role: Role.USER,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const user = {
|
|
82
|
+
id,
|
|
83
|
+
name: dto.name,
|
|
84
|
+
email: dto.email,
|
|
85
|
+
role: Role.USER,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// nestforge:feature:redis
|
|
89
|
+
await this.sendEmailVerification(
|
|
90
|
+
user.id,
|
|
91
|
+
user.email,
|
|
92
|
+
user.name,
|
|
93
|
+
);
|
|
94
|
+
// nestforge:feature:redis:end
|
|
95
|
+
|
|
96
|
+
return this.toAuthenticatedUser(user);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async login(dto: LoginDto) {
|
|
100
|
+
const user = await this.findUserByEmail(
|
|
101
|
+
dto.email,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
if (!user || !user.passwordHash) {
|
|
105
|
+
throw new UnauthorizedException(
|
|
106
|
+
'Credenciais inválidas',
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const passwordMatches = await bcrypt.compare(
|
|
111
|
+
dto.password,
|
|
112
|
+
user.passwordHash,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
if (!passwordMatches) {
|
|
116
|
+
throw new UnauthorizedException(
|
|
117
|
+
'Credenciais inválidas',
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return this.toAuthenticatedUser(user);
|
|
122
|
+
}
|
|
123
|
+
// nestforge:feature:auth:password:end
|
|
124
|
+
|
|
125
|
+
async validateOAuthLogin(
|
|
126
|
+
profile: OAuthProfile,
|
|
127
|
+
) {
|
|
128
|
+
const [linkedAccount] = await this.database
|
|
129
|
+
.select({
|
|
130
|
+
id: users.id,
|
|
131
|
+
email: users.email,
|
|
132
|
+
role: users.role,
|
|
133
|
+
})
|
|
134
|
+
.from(oauthAccounts)
|
|
135
|
+
.innerJoin(
|
|
136
|
+
users,
|
|
137
|
+
eq(oauthAccounts.userId, users.id),
|
|
138
|
+
)
|
|
139
|
+
.where(
|
|
140
|
+
and(
|
|
141
|
+
eq(
|
|
142
|
+
oauthAccounts.provider,
|
|
143
|
+
profile.provider,
|
|
144
|
+
),
|
|
145
|
+
eq(
|
|
146
|
+
oauthAccounts.providerUserId,
|
|
147
|
+
profile.providerUserId,
|
|
148
|
+
),
|
|
149
|
+
),
|
|
150
|
+
)
|
|
151
|
+
.limit(1);
|
|
152
|
+
|
|
153
|
+
if (linkedAccount) {
|
|
154
|
+
return this.toAuthenticatedUser(
|
|
155
|
+
linkedAccount,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let user = await this.findUserByEmail(
|
|
160
|
+
profile.email,
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
if (!user) {
|
|
164
|
+
const userId = randomUUID();
|
|
165
|
+
|
|
166
|
+
await this.database.insert(users).values({
|
|
167
|
+
id: userId,
|
|
168
|
+
name: profile.name,
|
|
169
|
+
email: profile.email,
|
|
170
|
+
passwordHash: null,
|
|
171
|
+
role: Role.USER,
|
|
172
|
+
emailVerifiedAt: new Date(),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
user = {
|
|
176
|
+
id: userId,
|
|
177
|
+
name: profile.name,
|
|
178
|
+
email: profile.email,
|
|
179
|
+
passwordHash: null,
|
|
180
|
+
role: Role.USER,
|
|
181
|
+
avatarUrl: null,
|
|
182
|
+
emailVerifiedAt: new Date(),
|
|
183
|
+
createdAt: new Date(),
|
|
184
|
+
updatedAt: new Date(),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
await this.database
|
|
189
|
+
.insert(oauthAccounts)
|
|
190
|
+
.values({
|
|
191
|
+
id: randomUUID(),
|
|
192
|
+
provider: profile.provider,
|
|
193
|
+
providerUserId:
|
|
194
|
+
profile.providerUserId,
|
|
195
|
+
userId: user.id,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
return this.toAuthenticatedUser(user);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// nestforge:feature:redis,auth:password
|
|
202
|
+
async forgotPassword(email: string) {
|
|
203
|
+
const user = await this.findUserByEmail(
|
|
204
|
+
email,
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const genericResponse = {
|
|
208
|
+
message:
|
|
209
|
+
'Se o e-mail existir, enviaremos instruções de redefinição de senha',
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
if (!user) {
|
|
213
|
+
return genericResponse;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const rawToken = randomBytes(32).toString(
|
|
217
|
+
'hex',
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
const expiresAt = new Date();
|
|
221
|
+
expiresAt.setHours(
|
|
222
|
+
expiresAt.getHours() + 1,
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
await this.database
|
|
226
|
+
.insert(passwordResetTokens)
|
|
227
|
+
.values({
|
|
228
|
+
id: randomUUID(),
|
|
229
|
+
tokenHash: this.hashToken(rawToken),
|
|
230
|
+
userId: user.id,
|
|
231
|
+
expiresAt,
|
|
232
|
+
usedAt: null,
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
await this.mailService.queuePasswordResetEmail(
|
|
236
|
+
user.email,
|
|
237
|
+
user.name,
|
|
238
|
+
rawToken,
|
|
239
|
+
);
|
|
240
|
+
|
|
241
|
+
return genericResponse;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async resetPassword(
|
|
245
|
+
rawToken: string,
|
|
246
|
+
newPassword: string,
|
|
247
|
+
) {
|
|
248
|
+
const tokenHash = this.hashToken(rawToken);
|
|
249
|
+
|
|
250
|
+
const [stored] = await this.database
|
|
251
|
+
.select()
|
|
252
|
+
.from(passwordResetTokens)
|
|
253
|
+
.where(
|
|
254
|
+
eq(
|
|
255
|
+
passwordResetTokens.tokenHash,
|
|
256
|
+
tokenHash,
|
|
257
|
+
),
|
|
258
|
+
)
|
|
259
|
+
.limit(1);
|
|
260
|
+
|
|
261
|
+
if (
|
|
262
|
+
!stored ||
|
|
263
|
+
stored.usedAt ||
|
|
264
|
+
stored.expiresAt < new Date()
|
|
265
|
+
) {
|
|
266
|
+
throw new UnauthorizedException(
|
|
267
|
+
'Token de redefinição inválido ou expirado',
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const passwordHash = await bcrypt.hash(
|
|
272
|
+
newPassword,
|
|
273
|
+
10,
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
// nestforge:feature:database:postgres
|
|
277
|
+
await this.database.transaction(
|
|
278
|
+
async (transaction) => {
|
|
279
|
+
await transaction
|
|
280
|
+
.update(users)
|
|
281
|
+
.set({
|
|
282
|
+
passwordHash,
|
|
283
|
+
updatedAt: new Date(),
|
|
284
|
+
})
|
|
285
|
+
.where(
|
|
286
|
+
eq(users.id, stored.userId),
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
await transaction
|
|
290
|
+
.update(passwordResetTokens)
|
|
291
|
+
.set({
|
|
292
|
+
usedAt: new Date(),
|
|
293
|
+
})
|
|
294
|
+
.where(
|
|
295
|
+
and(
|
|
296
|
+
eq(
|
|
297
|
+
passwordResetTokens.id,
|
|
298
|
+
stored.id,
|
|
299
|
+
),
|
|
300
|
+
isNull(
|
|
301
|
+
passwordResetTokens.usedAt,
|
|
302
|
+
),
|
|
303
|
+
),
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
// nestforge:feature:auth:token
|
|
307
|
+
await transaction
|
|
308
|
+
.update(refreshTokens)
|
|
309
|
+
.set({
|
|
310
|
+
revokedAt: new Date(),
|
|
311
|
+
})
|
|
312
|
+
.where(
|
|
313
|
+
and(
|
|
314
|
+
eq(
|
|
315
|
+
refreshTokens.userId,
|
|
316
|
+
stored.userId,
|
|
317
|
+
),
|
|
318
|
+
isNull(
|
|
319
|
+
refreshTokens.revokedAt,
|
|
320
|
+
),
|
|
321
|
+
),
|
|
322
|
+
);
|
|
323
|
+
// nestforge:feature:auth:token:end
|
|
324
|
+
},
|
|
325
|
+
);
|
|
326
|
+
// nestforge:feature:database:postgres:end
|
|
327
|
+
|
|
328
|
+
// nestforge:feature:database:mysql
|
|
329
|
+
await this.database.transaction(
|
|
330
|
+
async (transaction) => {
|
|
331
|
+
await transaction
|
|
332
|
+
.update(users)
|
|
333
|
+
.set({
|
|
334
|
+
passwordHash,
|
|
335
|
+
updatedAt: new Date(),
|
|
336
|
+
})
|
|
337
|
+
.where(
|
|
338
|
+
eq(users.id, stored.userId),
|
|
339
|
+
);
|
|
340
|
+
|
|
341
|
+
await transaction
|
|
342
|
+
.update(passwordResetTokens)
|
|
343
|
+
.set({
|
|
344
|
+
usedAt: new Date(),
|
|
345
|
+
})
|
|
346
|
+
.where(
|
|
347
|
+
and(
|
|
348
|
+
eq(
|
|
349
|
+
passwordResetTokens.id,
|
|
350
|
+
stored.id,
|
|
351
|
+
),
|
|
352
|
+
isNull(
|
|
353
|
+
passwordResetTokens.usedAt,
|
|
354
|
+
),
|
|
355
|
+
),
|
|
356
|
+
);
|
|
357
|
+
|
|
358
|
+
// nestforge:feature:auth:token
|
|
359
|
+
await transaction
|
|
360
|
+
.update(refreshTokens)
|
|
361
|
+
.set({
|
|
362
|
+
revokedAt: new Date(),
|
|
363
|
+
})
|
|
364
|
+
.where(
|
|
365
|
+
and(
|
|
366
|
+
eq(
|
|
367
|
+
refreshTokens.userId,
|
|
368
|
+
stored.userId,
|
|
369
|
+
),
|
|
370
|
+
isNull(
|
|
371
|
+
refreshTokens.revokedAt,
|
|
372
|
+
),
|
|
373
|
+
),
|
|
374
|
+
);
|
|
375
|
+
// nestforge:feature:auth:token:end
|
|
376
|
+
},
|
|
377
|
+
);
|
|
378
|
+
// nestforge:feature:database:mysql:end
|
|
379
|
+
|
|
380
|
+
// nestforge:feature:database:sqlite
|
|
381
|
+
this.database.transaction(
|
|
382
|
+
(transaction) => {
|
|
383
|
+
transaction
|
|
384
|
+
.update(users)
|
|
385
|
+
.set({
|
|
386
|
+
passwordHash,
|
|
387
|
+
updatedAt: new Date(),
|
|
388
|
+
})
|
|
389
|
+
.where(
|
|
390
|
+
eq(users.id, stored.userId),
|
|
391
|
+
)
|
|
392
|
+
.run();
|
|
393
|
+
|
|
394
|
+
transaction
|
|
395
|
+
.update(passwordResetTokens)
|
|
396
|
+
.set({
|
|
397
|
+
usedAt: new Date(),
|
|
398
|
+
})
|
|
399
|
+
.where(
|
|
400
|
+
and(
|
|
401
|
+
eq(
|
|
402
|
+
passwordResetTokens.id,
|
|
403
|
+
stored.id,
|
|
404
|
+
),
|
|
405
|
+
isNull(
|
|
406
|
+
passwordResetTokens.usedAt,
|
|
407
|
+
),
|
|
408
|
+
),
|
|
409
|
+
)
|
|
410
|
+
.run();
|
|
411
|
+
|
|
412
|
+
// nestforge:feature:auth:token
|
|
413
|
+
transaction
|
|
414
|
+
.update(refreshTokens)
|
|
415
|
+
.set({
|
|
416
|
+
revokedAt: new Date(),
|
|
417
|
+
})
|
|
418
|
+
.where(
|
|
419
|
+
and(
|
|
420
|
+
eq(
|
|
421
|
+
refreshTokens.userId,
|
|
422
|
+
stored.userId,
|
|
423
|
+
),
|
|
424
|
+
isNull(
|
|
425
|
+
refreshTokens.revokedAt,
|
|
426
|
+
),
|
|
427
|
+
),
|
|
428
|
+
)
|
|
429
|
+
.run();
|
|
430
|
+
// nestforge:feature:auth:token:end
|
|
431
|
+
},
|
|
432
|
+
);
|
|
433
|
+
// nestforge:feature:database:sqlite:end
|
|
434
|
+
|
|
435
|
+
return {
|
|
436
|
+
message: 'Senha redefinida com sucesso',
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
async verifyEmail(rawToken: string) {
|
|
441
|
+
const tokenHash = this.hashToken(rawToken);
|
|
442
|
+
|
|
443
|
+
const [stored] = await this.database
|
|
444
|
+
.select()
|
|
445
|
+
.from(emailVerificationTokens)
|
|
446
|
+
.where(
|
|
447
|
+
eq(
|
|
448
|
+
emailVerificationTokens.tokenHash,
|
|
449
|
+
tokenHash,
|
|
450
|
+
),
|
|
451
|
+
)
|
|
452
|
+
.limit(1);
|
|
453
|
+
|
|
454
|
+
if (
|
|
455
|
+
!stored ||
|
|
456
|
+
stored.usedAt ||
|
|
457
|
+
stored.expiresAt < new Date()
|
|
458
|
+
) {
|
|
459
|
+
throw new UnauthorizedException(
|
|
460
|
+
'Token de verificação inválido ou expirado',
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// nestforge:feature:database:postgres
|
|
465
|
+
await this.database.transaction(
|
|
466
|
+
async (transaction) => {
|
|
467
|
+
await transaction
|
|
468
|
+
.update(users)
|
|
469
|
+
.set({
|
|
470
|
+
emailVerifiedAt: new Date(),
|
|
471
|
+
updatedAt: new Date(),
|
|
472
|
+
})
|
|
473
|
+
.where(
|
|
474
|
+
eq(users.id, stored.userId),
|
|
475
|
+
);
|
|
476
|
+
|
|
477
|
+
await transaction
|
|
478
|
+
.update(emailVerificationTokens)
|
|
479
|
+
.set({
|
|
480
|
+
usedAt: new Date(),
|
|
481
|
+
})
|
|
482
|
+
.where(
|
|
483
|
+
and(
|
|
484
|
+
eq(
|
|
485
|
+
emailVerificationTokens.id,
|
|
486
|
+
stored.id,
|
|
487
|
+
),
|
|
488
|
+
isNull(
|
|
489
|
+
emailVerificationTokens.usedAt,
|
|
490
|
+
),
|
|
491
|
+
),
|
|
492
|
+
);
|
|
493
|
+
},
|
|
494
|
+
);
|
|
495
|
+
// nestforge:feature:database:postgres:end
|
|
496
|
+
|
|
497
|
+
// nestforge:feature:database:mysql
|
|
498
|
+
await this.database.transaction(
|
|
499
|
+
async (transaction) => {
|
|
500
|
+
await transaction
|
|
501
|
+
.update(users)
|
|
502
|
+
.set({
|
|
503
|
+
emailVerifiedAt: new Date(),
|
|
504
|
+
updatedAt: new Date(),
|
|
505
|
+
})
|
|
506
|
+
.where(
|
|
507
|
+
eq(users.id, stored.userId),
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
await transaction
|
|
511
|
+
.update(emailVerificationTokens)
|
|
512
|
+
.set({
|
|
513
|
+
usedAt: new Date(),
|
|
514
|
+
})
|
|
515
|
+
.where(
|
|
516
|
+
and(
|
|
517
|
+
eq(
|
|
518
|
+
emailVerificationTokens.id,
|
|
519
|
+
stored.id,
|
|
520
|
+
),
|
|
521
|
+
isNull(
|
|
522
|
+
emailVerificationTokens.usedAt,
|
|
523
|
+
),
|
|
524
|
+
),
|
|
525
|
+
);
|
|
526
|
+
},
|
|
527
|
+
);
|
|
528
|
+
// nestforge:feature:database:mysql:end
|
|
529
|
+
|
|
530
|
+
// nestforge:feature:database:sqlite
|
|
531
|
+
this.database.transaction(
|
|
532
|
+
(transaction) => {
|
|
533
|
+
transaction
|
|
534
|
+
.update(users)
|
|
535
|
+
.set({
|
|
536
|
+
emailVerifiedAt: new Date(),
|
|
537
|
+
updatedAt: new Date(),
|
|
538
|
+
})
|
|
539
|
+
.where(
|
|
540
|
+
eq(users.id, stored.userId),
|
|
541
|
+
)
|
|
542
|
+
.run();
|
|
543
|
+
|
|
544
|
+
transaction
|
|
545
|
+
.update(emailVerificationTokens)
|
|
546
|
+
.set({
|
|
547
|
+
usedAt: new Date(),
|
|
548
|
+
})
|
|
549
|
+
.where(
|
|
550
|
+
and(
|
|
551
|
+
eq(
|
|
552
|
+
emailVerificationTokens.id,
|
|
553
|
+
stored.id,
|
|
554
|
+
),
|
|
555
|
+
isNull(
|
|
556
|
+
emailVerificationTokens.usedAt,
|
|
557
|
+
),
|
|
558
|
+
),
|
|
559
|
+
)
|
|
560
|
+
.run();
|
|
561
|
+
},
|
|
562
|
+
);
|
|
563
|
+
// nestforge:feature:database:sqlite:end
|
|
564
|
+
|
|
565
|
+
return {
|
|
566
|
+
message: 'E-mail verificado com sucesso',
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
private async sendEmailVerification(
|
|
571
|
+
userId: string,
|
|
572
|
+
email: string,
|
|
573
|
+
name: string,
|
|
574
|
+
) {
|
|
575
|
+
const rawToken = randomBytes(32).toString(
|
|
576
|
+
'hex',
|
|
577
|
+
);
|
|
578
|
+
|
|
579
|
+
const expiresAt = new Date();
|
|
580
|
+
expiresAt.setHours(
|
|
581
|
+
expiresAt.getHours() + 24,
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
await this.database
|
|
585
|
+
.insert(emailVerificationTokens)
|
|
586
|
+
.values({
|
|
587
|
+
id: randomUUID(),
|
|
588
|
+
tokenHash: this.hashToken(rawToken),
|
|
589
|
+
userId,
|
|
590
|
+
expiresAt,
|
|
591
|
+
usedAt: null,
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
await this.mailService.queueVerificationEmail(
|
|
595
|
+
email,
|
|
596
|
+
name,
|
|
597
|
+
rawToken,
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
// nestforge:feature:redis,auth:password:end
|
|
601
|
+
|
|
602
|
+
private async findUserByEmail(
|
|
603
|
+
email: string,
|
|
604
|
+
) {
|
|
605
|
+
const [user] = await this.database
|
|
606
|
+
.select()
|
|
607
|
+
.from(users)
|
|
608
|
+
.where(eq(users.email, email))
|
|
609
|
+
.limit(1);
|
|
610
|
+
|
|
611
|
+
return user;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
private toAuthenticatedUser(user: {
|
|
615
|
+
id: string;
|
|
616
|
+
email: string;
|
|
617
|
+
role: string;
|
|
618
|
+
}) {
|
|
619
|
+
return {
|
|
620
|
+
id: user.id,
|
|
621
|
+
email: user.email,
|
|
622
|
+
role: user.role,
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
private hashToken(token: string): string {
|
|
627
|
+
return createHash('sha256')
|
|
628
|
+
.update(token)
|
|
629
|
+
.digest('hex');
|
|
630
|
+
}
|
|
631
|
+
}
|