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,346 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ConflictException,
|
|
3
|
+
Injectable,
|
|
4
|
+
UnauthorizedException,
|
|
5
|
+
} from '@nestjs/common';
|
|
6
|
+
import { InjectRepository } from '@nestjs/typeorm';
|
|
7
|
+
import {
|
|
8
|
+
DataSource,
|
|
9
|
+
IsNull,
|
|
10
|
+
Repository,
|
|
11
|
+
} from 'typeorm';
|
|
12
|
+
import * as bcrypt from 'bcryptjs';
|
|
13
|
+
import { createHash, randomBytes } from 'crypto';
|
|
14
|
+
import { UserEntity } from '../users/entities/user.entity';
|
|
15
|
+
import { OAuthAccountEntity } from './entities/oauth-account.entity';
|
|
16
|
+
|
|
17
|
+
// nestforge:feature:auth:token
|
|
18
|
+
import { RefreshTokenEntity } from './entities/refresh-token.entity';
|
|
19
|
+
// nestforge:feature:auth:token:end
|
|
20
|
+
|
|
21
|
+
// nestforge:feature:redis,auth:password
|
|
22
|
+
import { MailService } from '../mail/mail.service';
|
|
23
|
+
import { PasswordResetTokenEntity } from './entities/password-reset-token.entity';
|
|
24
|
+
import { EmailVerificationTokenEntity } from './entities/email-verification-token.entity';
|
|
25
|
+
// nestforge:feature:redis,auth:password:end
|
|
26
|
+
|
|
27
|
+
// nestforge:feature:auth:password
|
|
28
|
+
import { RegisterDto } from './dto/register.dto';
|
|
29
|
+
import { LoginDto } from './dto/login.dto';
|
|
30
|
+
// nestforge:feature:auth:password:end
|
|
31
|
+
|
|
32
|
+
import { OAuthProfile } from './strategies/google.strategy';
|
|
33
|
+
|
|
34
|
+
@Injectable()
|
|
35
|
+
export class AuthService {
|
|
36
|
+
constructor(
|
|
37
|
+
@InjectRepository(UserEntity)
|
|
38
|
+
private readonly usersRepository: Repository<UserEntity>,
|
|
39
|
+
|
|
40
|
+
@InjectRepository(OAuthAccountEntity)
|
|
41
|
+
private readonly oauthAccountsRepository: Repository<OAuthAccountEntity>,
|
|
42
|
+
|
|
43
|
+
private readonly dataSource: DataSource,
|
|
44
|
+
|
|
45
|
+
// nestforge:feature:redis,auth:password
|
|
46
|
+
@InjectRepository(PasswordResetTokenEntity)
|
|
47
|
+
private readonly passwordResetTokensRepository: Repository<PasswordResetTokenEntity>,
|
|
48
|
+
|
|
49
|
+
@InjectRepository(EmailVerificationTokenEntity)
|
|
50
|
+
private readonly emailVerificationTokensRepository: Repository<EmailVerificationTokenEntity>,
|
|
51
|
+
|
|
52
|
+
private readonly mailService: MailService,
|
|
53
|
+
// nestforge:feature:redis,auth:password:end
|
|
54
|
+
) { }
|
|
55
|
+
|
|
56
|
+
// nestforge:feature:auth:password
|
|
57
|
+
async register(dto: RegisterDto) {
|
|
58
|
+
const existing = await this.usersRepository.findOne({
|
|
59
|
+
where: { email: dto.email },
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (existing) {
|
|
63
|
+
throw new ConflictException('E-mail já cadastrado');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const passwordHash = await bcrypt.hash(dto.password, 10);
|
|
67
|
+
|
|
68
|
+
const user = this.usersRepository.create({
|
|
69
|
+
name: dto.name,
|
|
70
|
+
email: dto.email,
|
|
71
|
+
passwordHash,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const savedUser = await this.usersRepository.save(user);
|
|
75
|
+
|
|
76
|
+
// nestforge:feature:redis
|
|
77
|
+
await this.sendEmailVerification(
|
|
78
|
+
savedUser.id,
|
|
79
|
+
savedUser.email,
|
|
80
|
+
savedUser.name,
|
|
81
|
+
);
|
|
82
|
+
// nestforge:feature:redis:end
|
|
83
|
+
|
|
84
|
+
return this.toAuthenticatedUser(savedUser);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async login(dto: LoginDto) {
|
|
88
|
+
const user = await this.usersRepository.findOne({
|
|
89
|
+
where: { email: dto.email },
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (!user || !user.passwordHash) {
|
|
93
|
+
throw new UnauthorizedException(
|
|
94
|
+
'Credenciais inválidas',
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const passwordMatches = await bcrypt.compare(
|
|
99
|
+
dto.password,
|
|
100
|
+
user.passwordHash,
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (!passwordMatches) {
|
|
104
|
+
throw new UnauthorizedException(
|
|
105
|
+
'Credenciais inválidas',
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return this.toAuthenticatedUser(user);
|
|
110
|
+
}
|
|
111
|
+
// nestforge:feature:auth:password:end
|
|
112
|
+
|
|
113
|
+
async validateOAuthLogin(profile: OAuthProfile) {
|
|
114
|
+
const linkedAccount =
|
|
115
|
+
await this.oauthAccountsRepository.findOne({
|
|
116
|
+
where: {
|
|
117
|
+
provider: profile.provider,
|
|
118
|
+
providerUserId: profile.providerUserId,
|
|
119
|
+
},
|
|
120
|
+
relations: {
|
|
121
|
+
user: true,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
if (linkedAccount) {
|
|
126
|
+
return this.toAuthenticatedUser(
|
|
127
|
+
linkedAccount.user,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let user = await this.usersRepository.findOne({
|
|
132
|
+
where: { email: profile.email },
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
if (!user) {
|
|
136
|
+
const newUser = this.usersRepository.create({
|
|
137
|
+
name: profile.name,
|
|
138
|
+
email: profile.email,
|
|
139
|
+
emailVerifiedAt: new Date(),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
user = await this.usersRepository.save(newUser);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const oauthAccount =
|
|
146
|
+
this.oauthAccountsRepository.create({
|
|
147
|
+
provider: profile.provider,
|
|
148
|
+
providerUserId: profile.providerUserId,
|
|
149
|
+
userId: user.id,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
await this.oauthAccountsRepository.save(
|
|
153
|
+
oauthAccount,
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
return this.toAuthenticatedUser(user);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// nestforge:feature:redis,auth:password
|
|
160
|
+
async forgotPassword(email: string) {
|
|
161
|
+
const user = await this.usersRepository.findOne({
|
|
162
|
+
where: { email },
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const genericResponse = {
|
|
166
|
+
message:
|
|
167
|
+
'Se o e-mail existir, enviaremos instruções de redefinição de senha',
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
if (!user) {
|
|
171
|
+
return genericResponse;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const rawToken = randomBytes(32).toString('hex');
|
|
175
|
+
const expiresAt = new Date();
|
|
176
|
+
|
|
177
|
+
expiresAt.setHours(expiresAt.getHours() + 1);
|
|
178
|
+
|
|
179
|
+
const resetToken =
|
|
180
|
+
this.passwordResetTokensRepository.create({
|
|
181
|
+
tokenHash: this.hashToken(rawToken),
|
|
182
|
+
userId: user.id,
|
|
183
|
+
expiresAt,
|
|
184
|
+
usedAt: null,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
await this.passwordResetTokensRepository.save(
|
|
188
|
+
resetToken,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
await this.mailService.queuePasswordResetEmail(
|
|
192
|
+
user.email,
|
|
193
|
+
user.name,
|
|
194
|
+
rawToken,
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
return genericResponse;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async resetPassword(
|
|
201
|
+
rawToken: string,
|
|
202
|
+
newPassword: string,
|
|
203
|
+
) {
|
|
204
|
+
const tokenHash = this.hashToken(rawToken);
|
|
205
|
+
|
|
206
|
+
const stored =
|
|
207
|
+
await this.passwordResetTokensRepository.findOne({
|
|
208
|
+
where: { tokenHash },
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
if (
|
|
212
|
+
!stored ||
|
|
213
|
+
stored.usedAt ||
|
|
214
|
+
stored.expiresAt < new Date()
|
|
215
|
+
) {
|
|
216
|
+
throw new UnauthorizedException(
|
|
217
|
+
'Token de redefinição inválido ou expirado',
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const passwordHash = await bcrypt.hash(
|
|
222
|
+
newPassword,
|
|
223
|
+
10,
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
await this.dataSource.transaction(
|
|
227
|
+
async (manager) => {
|
|
228
|
+
await manager.update(
|
|
229
|
+
UserEntity,
|
|
230
|
+
{ id: stored.userId },
|
|
231
|
+
{ passwordHash },
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
await manager.update(
|
|
235
|
+
PasswordResetTokenEntity,
|
|
236
|
+
{ id: stored.id },
|
|
237
|
+
{ usedAt: new Date() },
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
// nestforge:feature:auth:token
|
|
241
|
+
await manager.update(
|
|
242
|
+
RefreshTokenEntity,
|
|
243
|
+
{
|
|
244
|
+
userId: stored.userId,
|
|
245
|
+
revokedAt: IsNull(),
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
revokedAt: new Date(),
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
// nestforge:feature:auth:token:end
|
|
252
|
+
},
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
return {
|
|
256
|
+
message: 'Senha redefinida com sucesso',
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async verifyEmail(rawToken: string) {
|
|
261
|
+
const tokenHash = this.hashToken(rawToken);
|
|
262
|
+
|
|
263
|
+
const stored =
|
|
264
|
+
await this.emailVerificationTokensRepository.findOne({
|
|
265
|
+
where: { tokenHash },
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
if (
|
|
269
|
+
!stored ||
|
|
270
|
+
stored.usedAt ||
|
|
271
|
+
stored.expiresAt < new Date()
|
|
272
|
+
) {
|
|
273
|
+
throw new UnauthorizedException(
|
|
274
|
+
'Token de verificação inválido ou expirado',
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
await this.dataSource.transaction(
|
|
279
|
+
async (manager) => {
|
|
280
|
+
await manager.update(
|
|
281
|
+
UserEntity,
|
|
282
|
+
{ id: stored.userId },
|
|
283
|
+
{ emailVerifiedAt: new Date() },
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
await manager.update(
|
|
287
|
+
EmailVerificationTokenEntity,
|
|
288
|
+
{ id: stored.id },
|
|
289
|
+
{ usedAt: new Date() },
|
|
290
|
+
);
|
|
291
|
+
},
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
message: 'E-mail verificado com sucesso',
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private async sendEmailVerification(
|
|
300
|
+
userId: string,
|
|
301
|
+
email: string,
|
|
302
|
+
name: string,
|
|
303
|
+
) {
|
|
304
|
+
const rawToken = randomBytes(32).toString('hex');
|
|
305
|
+
const expiresAt = new Date();
|
|
306
|
+
|
|
307
|
+
expiresAt.setHours(expiresAt.getHours() + 24);
|
|
308
|
+
|
|
309
|
+
const verificationToken =
|
|
310
|
+
this.emailVerificationTokensRepository.create({
|
|
311
|
+
tokenHash: this.hashToken(rawToken),
|
|
312
|
+
userId,
|
|
313
|
+
expiresAt,
|
|
314
|
+
usedAt: null,
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
await this.emailVerificationTokensRepository.save(
|
|
318
|
+
verificationToken,
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
await this.mailService.queueVerificationEmail(
|
|
322
|
+
email,
|
|
323
|
+
name,
|
|
324
|
+
rawToken,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
// nestforge:feature:redis,auth:password:end
|
|
328
|
+
|
|
329
|
+
private toAuthenticatedUser(user: {
|
|
330
|
+
id: string;
|
|
331
|
+
email: string;
|
|
332
|
+
role: string;
|
|
333
|
+
}) {
|
|
334
|
+
return {
|
|
335
|
+
id: user.id,
|
|
336
|
+
email: user.email,
|
|
337
|
+
role: user.role,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private hashToken(token: string): string {
|
|
342
|
+
return createHash('sha256')
|
|
343
|
+
.update(token)
|
|
344
|
+
.digest('hex');
|
|
345
|
+
}
|
|
346
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// nestforge:feature-file:redis,auth:password
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { createZodDto } from 'nestjs-zod';
|
|
4
|
+
|
|
5
|
+
export const forgotPasswordSchema = z.object({
|
|
6
|
+
email: z.string().email().describe('E-mail da conta a ser recuperada'),
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export class ForgotPasswordDto extends createZodDto(forgotPasswordSchema) { }
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// nestforge:feature-file:auth:password
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { createZodDto } from 'nestjs-zod';
|
|
4
|
+
|
|
5
|
+
export const loginSchema = z.object({
|
|
6
|
+
email: z.string().email().describe('E-mail do usuário'),
|
|
7
|
+
password: z.string().min(8).describe('Senha'),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export class LoginDto extends createZodDto(loginSchema) { }
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// nestforge:feature-file:auth:token
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { createZodDto } from 'nestjs-zod';
|
|
4
|
+
|
|
5
|
+
export const refreshTokenSchema = z.object({
|
|
6
|
+
refreshToken: z.string().describe('Refresh token emitido no login'),
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export class RefreshTokenDto extends createZodDto(refreshTokenSchema) { }
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// nestforge:feature-file:auth:password
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { createZodDto } from 'nestjs-zod';
|
|
4
|
+
|
|
5
|
+
export const registerSchema = z.object({
|
|
6
|
+
name: z.string().min(2).describe('Nome completo do usuário'),
|
|
7
|
+
email: z.string().email().describe('E-mail do usuário'),
|
|
8
|
+
password: z.string().min(8).describe('Senha (mínimo 8 caracteres)'),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
export class RegisterDto extends createZodDto(registerSchema) { }
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// nestforge:feature-file:redis,auth:password
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { createZodDto } from 'nestjs-zod';
|
|
4
|
+
|
|
5
|
+
export const resetPasswordSchema = z.object({
|
|
6
|
+
token: z.string().describe('Token recebido por e-mail'),
|
|
7
|
+
password: z.string().min(8).describe('Nova senha (mínimo 8 caracteres)'),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export class ResetPasswordDto extends createZodDto(resetPasswordSchema) { }
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// nestforge:feature-file:redis,auth:password
|
|
2
|
+
import {
|
|
3
|
+
Column,
|
|
4
|
+
CreateDateColumn,
|
|
5
|
+
Entity,
|
|
6
|
+
JoinColumn,
|
|
7
|
+
ManyToOne,
|
|
8
|
+
PrimaryGeneratedColumn,
|
|
9
|
+
} from 'typeorm';
|
|
10
|
+
import { UserEntity } from '../../users/entities/user.entity';
|
|
11
|
+
|
|
12
|
+
@Entity({ name: 'email_verification_tokens' })
|
|
13
|
+
export class EmailVerificationTokenEntity {
|
|
14
|
+
@PrimaryGeneratedColumn('uuid')
|
|
15
|
+
id!: string;
|
|
16
|
+
|
|
17
|
+
@Column({
|
|
18
|
+
name: 'token_hash',
|
|
19
|
+
type: 'varchar',
|
|
20
|
+
length: 64,
|
|
21
|
+
unique: true,
|
|
22
|
+
})
|
|
23
|
+
tokenHash!: string;
|
|
24
|
+
|
|
25
|
+
@Column({
|
|
26
|
+
name: 'user_id',
|
|
27
|
+
type: 'varchar',
|
|
28
|
+
length: 36,
|
|
29
|
+
})
|
|
30
|
+
userId!: string;
|
|
31
|
+
|
|
32
|
+
@ManyToOne(() => UserEntity, {
|
|
33
|
+
onDelete: 'CASCADE',
|
|
34
|
+
})
|
|
35
|
+
@JoinColumn({ name: 'user_id' })
|
|
36
|
+
user!: UserEntity;
|
|
37
|
+
|
|
38
|
+
@Column({
|
|
39
|
+
name: 'expires_at',
|
|
40
|
+
// nestforge:feature:database:postgres
|
|
41
|
+
type: 'timestamp with time zone',
|
|
42
|
+
// nestforge:feature:database:postgres:end
|
|
43
|
+
// nestforge:feature:database:mysql
|
|
44
|
+
type: 'datetime',
|
|
45
|
+
// nestforge:feature:database:mysql:end
|
|
46
|
+
// nestforge:feature:database:sqlite
|
|
47
|
+
type: 'datetime',
|
|
48
|
+
// nestforge:feature:database:sqlite:end
|
|
49
|
+
})
|
|
50
|
+
expiresAt!: Date;
|
|
51
|
+
|
|
52
|
+
@Column({
|
|
53
|
+
name: 'used_at',
|
|
54
|
+
nullable: true,
|
|
55
|
+
// nestforge:feature:database:postgres
|
|
56
|
+
type: 'timestamp with time zone',
|
|
57
|
+
// nestforge:feature:database:postgres:end
|
|
58
|
+
// nestforge:feature:database:mysql
|
|
59
|
+
type: 'datetime',
|
|
60
|
+
// nestforge:feature:database:mysql:end
|
|
61
|
+
// nestforge:feature:database:sqlite
|
|
62
|
+
type: 'datetime',
|
|
63
|
+
// nestforge:feature:database:sqlite:end
|
|
64
|
+
})
|
|
65
|
+
usedAt!: Date | null;
|
|
66
|
+
|
|
67
|
+
@CreateDateColumn({ name: 'created_at' })
|
|
68
|
+
createdAt!: Date;
|
|
69
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Column,
|
|
3
|
+
CreateDateColumn,
|
|
4
|
+
Entity,
|
|
5
|
+
Index,
|
|
6
|
+
JoinColumn,
|
|
7
|
+
ManyToOne,
|
|
8
|
+
PrimaryGeneratedColumn,
|
|
9
|
+
} from 'typeorm';
|
|
10
|
+
import { UserEntity } from '../../users/entities/user.entity';
|
|
11
|
+
|
|
12
|
+
@Entity({ name: 'oauth_accounts' })
|
|
13
|
+
@Index(
|
|
14
|
+
'oauth_accounts_provider_provider_user_id_key',
|
|
15
|
+
['provider', 'providerUserId'],
|
|
16
|
+
{ unique: true },
|
|
17
|
+
)
|
|
18
|
+
export class OAuthAccountEntity {
|
|
19
|
+
@PrimaryGeneratedColumn('uuid')
|
|
20
|
+
id!: string;
|
|
21
|
+
|
|
22
|
+
@Column({
|
|
23
|
+
type: 'varchar',
|
|
24
|
+
length: 32,
|
|
25
|
+
})
|
|
26
|
+
provider!: string;
|
|
27
|
+
|
|
28
|
+
@Column({
|
|
29
|
+
name: 'provider_user_id',
|
|
30
|
+
type: 'varchar',
|
|
31
|
+
length: 255,
|
|
32
|
+
})
|
|
33
|
+
providerUserId!: string;
|
|
34
|
+
|
|
35
|
+
@Column({
|
|
36
|
+
name: 'user_id',
|
|
37
|
+
type: 'varchar',
|
|
38
|
+
length: 36,
|
|
39
|
+
})
|
|
40
|
+
userId!: string;
|
|
41
|
+
|
|
42
|
+
@ManyToOne(() => UserEntity, {
|
|
43
|
+
onDelete: 'CASCADE',
|
|
44
|
+
})
|
|
45
|
+
@JoinColumn({ name: 'user_id' })
|
|
46
|
+
user!: UserEntity;
|
|
47
|
+
|
|
48
|
+
@CreateDateColumn({ name: 'created_at' })
|
|
49
|
+
createdAt!: Date;
|
|
50
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// nestforge:feature-file:redis,auth:password
|
|
2
|
+
import {
|
|
3
|
+
Column,
|
|
4
|
+
CreateDateColumn,
|
|
5
|
+
Entity,
|
|
6
|
+
JoinColumn,
|
|
7
|
+
ManyToOne,
|
|
8
|
+
PrimaryGeneratedColumn,
|
|
9
|
+
} from 'typeorm';
|
|
10
|
+
import { UserEntity } from '../../users/entities/user.entity';
|
|
11
|
+
|
|
12
|
+
@Entity({ name: 'password_reset_tokens' })
|
|
13
|
+
export class PasswordResetTokenEntity {
|
|
14
|
+
@PrimaryGeneratedColumn('uuid')
|
|
15
|
+
id!: string;
|
|
16
|
+
|
|
17
|
+
@Column({
|
|
18
|
+
name: 'token_hash',
|
|
19
|
+
type: 'varchar',
|
|
20
|
+
length: 64,
|
|
21
|
+
unique: true,
|
|
22
|
+
})
|
|
23
|
+
tokenHash!: string;
|
|
24
|
+
|
|
25
|
+
@Column({
|
|
26
|
+
name: 'user_id',
|
|
27
|
+
type: 'varchar',
|
|
28
|
+
length: 36,
|
|
29
|
+
})
|
|
30
|
+
userId!: string;
|
|
31
|
+
|
|
32
|
+
@ManyToOne(() => UserEntity, {
|
|
33
|
+
onDelete: 'CASCADE',
|
|
34
|
+
})
|
|
35
|
+
@JoinColumn({ name: 'user_id' })
|
|
36
|
+
user!: UserEntity;
|
|
37
|
+
|
|
38
|
+
@Column({
|
|
39
|
+
name: 'expires_at',
|
|
40
|
+
// nestforge:feature:database:postgres
|
|
41
|
+
type: 'timestamp with time zone',
|
|
42
|
+
// nestforge:feature:database:postgres:end
|
|
43
|
+
// nestforge:feature:database:mysql
|
|
44
|
+
type: 'datetime',
|
|
45
|
+
// nestforge:feature:database:mysql:end
|
|
46
|
+
// nestforge:feature:database:sqlite
|
|
47
|
+
type: 'datetime',
|
|
48
|
+
// nestforge:feature:database:sqlite:end
|
|
49
|
+
})
|
|
50
|
+
expiresAt!: Date;
|
|
51
|
+
|
|
52
|
+
@Column({
|
|
53
|
+
name: 'used_at',
|
|
54
|
+
nullable: true,
|
|
55
|
+
// nestforge:feature:database:postgres
|
|
56
|
+
type: 'timestamp with time zone',
|
|
57
|
+
// nestforge:feature:database:postgres:end
|
|
58
|
+
// nestforge:feature:database:mysql
|
|
59
|
+
type: 'datetime',
|
|
60
|
+
// nestforge:feature:database:mysql:end
|
|
61
|
+
// nestforge:feature:database:sqlite
|
|
62
|
+
type: 'datetime',
|
|
63
|
+
// nestforge:feature:database:sqlite:end
|
|
64
|
+
})
|
|
65
|
+
usedAt!: Date | null;
|
|
66
|
+
|
|
67
|
+
@CreateDateColumn({ name: 'created_at' })
|
|
68
|
+
createdAt!: Date;
|
|
69
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// nestforge:feature-file:auth:token
|
|
2
|
+
import {
|
|
3
|
+
Column,
|
|
4
|
+
CreateDateColumn,
|
|
5
|
+
Entity,
|
|
6
|
+
JoinColumn,
|
|
7
|
+
ManyToOne,
|
|
8
|
+
PrimaryGeneratedColumn,
|
|
9
|
+
} from 'typeorm';
|
|
10
|
+
import { UserEntity } from '../../users/entities/user.entity';
|
|
11
|
+
|
|
12
|
+
@Entity({ name: 'refresh_tokens' })
|
|
13
|
+
export class RefreshTokenEntity {
|
|
14
|
+
@PrimaryGeneratedColumn('uuid')
|
|
15
|
+
id!: string;
|
|
16
|
+
|
|
17
|
+
@Column({
|
|
18
|
+
name: 'token_hash',
|
|
19
|
+
type: 'varchar',
|
|
20
|
+
length: 64,
|
|
21
|
+
unique: true,
|
|
22
|
+
})
|
|
23
|
+
tokenHash!: string;
|
|
24
|
+
|
|
25
|
+
@Column({
|
|
26
|
+
name: 'user_id',
|
|
27
|
+
type: 'varchar',
|
|
28
|
+
length: 36,
|
|
29
|
+
})
|
|
30
|
+
userId!: string;
|
|
31
|
+
|
|
32
|
+
@ManyToOne(() => UserEntity, {
|
|
33
|
+
onDelete: 'CASCADE',
|
|
34
|
+
})
|
|
35
|
+
@JoinColumn({ name: 'user_id' })
|
|
36
|
+
user!: UserEntity;
|
|
37
|
+
|
|
38
|
+
@Column({
|
|
39
|
+
name: 'expires_at',
|
|
40
|
+
// nestforge:feature:database:postgres
|
|
41
|
+
type: 'timestamp with time zone',
|
|
42
|
+
// nestforge:feature:database:postgres:end
|
|
43
|
+
// nestforge:feature:database:mysql
|
|
44
|
+
type: 'datetime',
|
|
45
|
+
// nestforge:feature:database:mysql:end
|
|
46
|
+
// nestforge:feature:database:sqlite
|
|
47
|
+
type: 'datetime',
|
|
48
|
+
// nestforge:feature:database:sqlite:end
|
|
49
|
+
})
|
|
50
|
+
expiresAt!: Date;
|
|
51
|
+
|
|
52
|
+
@Column({
|
|
53
|
+
name: 'revoked_at',
|
|
54
|
+
nullable: true,
|
|
55
|
+
// nestforge:feature:database:postgres
|
|
56
|
+
type: 'timestamp with time zone',
|
|
57
|
+
// nestforge:feature:database:postgres:end
|
|
58
|
+
// nestforge:feature:database:mysql
|
|
59
|
+
type: 'datetime',
|
|
60
|
+
// nestforge:feature:database:mysql:end
|
|
61
|
+
// nestforge:feature:database:sqlite
|
|
62
|
+
type: 'datetime',
|
|
63
|
+
// nestforge:feature:database:sqlite:end
|
|
64
|
+
})
|
|
65
|
+
revokedAt!: Date | null;
|
|
66
|
+
|
|
67
|
+
@CreateDateColumn({ name: 'created_at' })
|
|
68
|
+
createdAt!: Date;
|
|
69
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// nestforge:feature-file:auth:session
|
|
2
|
+
import { ISession } from 'connect-typeorm';
|
|
3
|
+
import {
|
|
4
|
+
Column,
|
|
5
|
+
DeleteDateColumn,
|
|
6
|
+
Entity,
|
|
7
|
+
Index,
|
|
8
|
+
PrimaryColumn,
|
|
9
|
+
ValueTransformer,
|
|
10
|
+
} from 'typeorm';
|
|
11
|
+
|
|
12
|
+
const bigintTransformer: ValueTransformer = {
|
|
13
|
+
to: (value: number) => value,
|
|
14
|
+
from: (value: string | number) => Number(value),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
@Entity({ name: 'sessions' })
|
|
18
|
+
export class SessionEntity implements ISession {
|
|
19
|
+
@Index()
|
|
20
|
+
@Column({
|
|
21
|
+
name: 'expired_at',
|
|
22
|
+
type: 'bigint',
|
|
23
|
+
transformer: bigintTransformer,
|
|
24
|
+
})
|
|
25
|
+
expiredAt = Date.now();
|
|
26
|
+
|
|
27
|
+
@PrimaryColumn({
|
|
28
|
+
type: 'varchar',
|
|
29
|
+
length: 255,
|
|
30
|
+
})
|
|
31
|
+
id = '';
|
|
32
|
+
|
|
33
|
+
@Column({ type: 'text' })
|
|
34
|
+
json = '';
|
|
35
|
+
|
|
36
|
+
@DeleteDateColumn({
|
|
37
|
+
name: 'destroyed_at',
|
|
38
|
+
nullable: true,
|
|
39
|
+
// nestforge:feature:database:postgres
|
|
40
|
+
type: 'timestamp with time zone',
|
|
41
|
+
// nestforge:feature:database:postgres:end
|
|
42
|
+
// nestforge:feature:database:mysql
|
|
43
|
+
type: 'datetime',
|
|
44
|
+
// nestforge:feature:database:mysql:end
|
|
45
|
+
// nestforge:feature:database:sqlite
|
|
46
|
+
type: 'datetime',
|
|
47
|
+
// nestforge:feature:database:sqlite:end
|
|
48
|
+
})
|
|
49
|
+
destroyedAt?: Date;
|
|
50
|
+
}
|