ar-saas 0.3.1 → 0.3.3
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 -21
- package/README.md +338 -314
- package/dist/cli.js +19 -0
- package/dist/generator.js +166 -55
- package/package.json +52 -50
- package/templates/backend/.env.example +67 -67
- package/templates/backend/.prettierrc +4 -4
- package/templates/backend/README.md +249 -168
- package/templates/backend/eslint.config.mjs +35 -35
- package/templates/backend/nest-cli.json +8 -8
- package/templates/backend/package-lock.json +10979 -10979
- package/templates/backend/package.json +88 -88
- package/templates/backend/src/app.controller.spec.ts +24 -24
- package/templates/backend/src/app.controller.ts +15 -15
- package/templates/backend/src/app.module.ts +40 -40
- package/templates/backend/src/app.service.ts +11 -11
- package/templates/backend/src/common/base/base.repository.ts +221 -221
- package/templates/backend/src/common/base/base.schema.ts +24 -24
- package/templates/backend/src/common/decorators/cookie.decorator.ts +9 -9
- package/templates/backend/src/common/decorators/current-user.decorator.ts +20 -20
- package/templates/backend/src/common/decorators/workspace-id.decorator.ts +14 -14
- package/templates/backend/src/common/filters/global-exception.filter.ts +61 -61
- package/templates/backend/src/common/guards/jwt-auth.guard.ts +5 -5
- package/templates/backend/src/common/interceptors/workspace-tenant.interceptor.ts +45 -45
- package/templates/backend/src/main.ts +51 -51
- package/templates/backend/src/modules/auth/auth.controller.ts +158 -158
- package/templates/backend/src/modules/auth/auth.module.ts +20 -20
- package/templates/backend/src/modules/auth/auth.service.ts +257 -257
- package/templates/backend/src/modules/auth/dto/forgot-password.dto.ts +9 -9
- package/templates/backend/src/modules/auth/dto/login.dto.ts +14 -14
- package/templates/backend/src/modules/auth/dto/refresh-token.dto.ts +12 -12
- package/templates/backend/src/modules/auth/dto/register.dto.ts +26 -26
- package/templates/backend/src/modules/auth/dto/reset-password.dto.ts +16 -16
- package/templates/backend/src/modules/auth/dto/verify-email.dto.ts +9 -9
- package/templates/backend/src/modules/auth/strategies/jwt.strategy.ts +43 -43
- package/templates/backend/src/modules/mail/mail.module.ts +9 -9
- package/templates/backend/src/modules/mail/mail.service.ts +141 -141
- package/templates/backend/src/modules/users/schemas/user.schema.ts +54 -54
- package/templates/backend/src/modules/users/users.module.ts +14 -14
- package/templates/backend/src/modules/users/users.repository.ts +51 -51
- package/templates/backend/src/modules/users/users.service.ts +104 -104
- package/templates/backend/src/modules/workspaces/schemas/workspace.schema.ts +26 -26
- package/templates/backend/src/modules/workspaces/workspaces.module.ts +16 -16
- package/templates/backend/src/modules/workspaces/workspaces.repository.ts +34 -34
- package/templates/backend/src/modules/workspaces/workspaces.service.ts +42 -42
- package/templates/backend/test/app.e2e-spec.ts +25 -25
- package/templates/backend/test/jest-e2e.json +9 -9
- package/templates/backend/tsconfig.build.json +4 -4
- package/templates/backend/tsconfig.json +26 -26
- package/templates/frontend/.env.local.example +1 -1
- package/templates/frontend/README.md +152 -0
- package/templates/frontend/components.json +20 -20
- package/templates/frontend/eslint.config.mjs +14 -14
- package/templates/frontend/next.config.ts +5 -5
- package/templates/frontend/package-lock.json +6722 -6722
- package/templates/frontend/package.json +48 -48
- package/templates/frontend/pnpm-lock.yaml +5012 -5012
- package/templates/frontend/pnpm-workspace.yaml +3 -3
- package/templates/frontend/postcss.config.mjs +7 -7
- package/templates/frontend/src/app/(auth)/forgot-password/page.tsx +84 -84
- package/templates/frontend/src/app/(auth)/layout.tsx +28 -28
- package/templates/frontend/src/app/(auth)/login/page.tsx +111 -111
- package/templates/frontend/src/app/(auth)/register/page.tsx +161 -161
- package/templates/frontend/src/app/(auth)/reset-password/page.tsx +120 -120
- package/templates/frontend/src/app/(auth)/verify-email/page.tsx +78 -78
- package/templates/frontend/src/app/(dashboard)/billing/page.tsx +111 -111
- package/templates/frontend/src/app/(dashboard)/dashboard/page.tsx +105 -105
- package/templates/frontend/src/app/(dashboard)/layout.tsx +38 -38
- package/templates/frontend/src/app/(dashboard)/profile/page.tsx +226 -226
- package/templates/frontend/src/app/(dashboard)/settings/page.tsx +156 -156
- package/templates/frontend/src/app/(dashboard)/team/page.tsx +178 -178
- package/templates/frontend/src/app/(legal)/privacy/page.tsx +127 -127
- package/templates/frontend/src/app/(legal)/terms/page.tsx +118 -118
- package/templates/frontend/src/app/globals.css +81 -81
- package/templates/frontend/src/app/layout.tsx +26 -26
- package/templates/frontend/src/app/page.tsx +5 -45
- package/templates/frontend/src/app/setup/page.tsx +371 -275
- package/templates/frontend/src/components/dashboard/header.tsx +89 -89
- package/templates/frontend/src/components/dashboard/sidebar.tsx +71 -71
- package/templates/frontend/src/components/dashboard/stat-card.tsx +34 -34
- package/templates/frontend/src/components/landing/faq.tsx +39 -39
- package/templates/frontend/src/components/landing/features.tsx +54 -54
- package/templates/frontend/src/components/landing/footer.tsx +76 -76
- package/templates/frontend/src/components/landing/hero.tsx +72 -72
- package/templates/frontend/src/components/landing/navbar.tsx +78 -78
- package/templates/frontend/src/components/landing/pricing.tsx +90 -90
- package/templates/frontend/src/components/ui/accordion.tsx +52 -52
- package/templates/frontend/src/components/ui/avatar.tsx +46 -46
- package/templates/frontend/src/components/ui/badge.tsx +30 -30
- package/templates/frontend/src/components/ui/button.tsx +52 -52
- package/templates/frontend/src/components/ui/card.tsx +50 -50
- package/templates/frontend/src/components/ui/checkbox.tsx +27 -27
- package/templates/frontend/src/components/ui/dialog.tsx +100 -100
- package/templates/frontend/src/components/ui/dropdown-menu.tsx +173 -173
- package/templates/frontend/src/components/ui/form.tsx +158 -158
- package/templates/frontend/src/components/ui/input.tsx +21 -21
- package/templates/frontend/src/components/ui/label.tsx +22 -22
- package/templates/frontend/src/components/ui/separator.tsx +25 -25
- package/templates/frontend/src/components/ui/skeleton.tsx +7 -7
- package/templates/frontend/src/components/ui/switch.tsx +28 -28
- package/templates/frontend/src/components/ui/tabs.tsx +54 -54
- package/templates/frontend/src/components/ui/textarea.tsx +20 -20
- package/templates/frontend/src/components/ui/toast.tsx +109 -109
- package/templates/frontend/src/components/ui/toaster.tsx +30 -30
- package/templates/frontend/src/config/site.ts +197 -197
- package/templates/frontend/src/hooks/use-toast.ts +116 -116
- package/templates/frontend/src/lib/api/auth.ts +39 -39
- package/templates/frontend/src/lib/api/client.ts +66 -66
- package/templates/frontend/src/lib/hooks/use-auth.ts +1 -1
- package/templates/frontend/src/lib/utils.ts +6 -6
- package/templates/frontend/src/providers/auth-provider.tsx +60 -60
- package/templates/frontend/src/types/api.ts +12 -12
- package/templates/frontend/src/types/auth.ts +27 -27
- package/templates/frontend/tsconfig.json +23 -23
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ExceptionFilter,
|
|
3
|
-
Catch,
|
|
4
|
-
ArgumentsHost,
|
|
5
|
-
HttpException,
|
|
6
|
-
HttpStatus,
|
|
7
|
-
Logger,
|
|
8
|
-
} from '@nestjs/common';
|
|
9
|
-
import { Request, Response } from 'express';
|
|
10
|
-
|
|
11
|
-
@Catch()
|
|
12
|
-
export class GlobalExceptionFilter implements ExceptionFilter {
|
|
13
|
-
private readonly logger = new Logger(GlobalExceptionFilter.name);
|
|
14
|
-
|
|
15
|
-
catch(exception: unknown, host: ArgumentsHost): void {
|
|
16
|
-
const ctx = host.switchToHttp();
|
|
17
|
-
const response = ctx.getResponse<Response>();
|
|
18
|
-
const request = ctx.getRequest<Request>();
|
|
19
|
-
|
|
20
|
-
let status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
21
|
-
let message = 'Error interno del servidor';
|
|
22
|
-
let error = 'Internal Server Error';
|
|
23
|
-
|
|
24
|
-
if (exception instanceof HttpException) {
|
|
25
|
-
status = exception.getStatus();
|
|
26
|
-
const exceptionResponse = exception.getResponse();
|
|
27
|
-
|
|
28
|
-
if (typeof exceptionResponse === 'string') {
|
|
29
|
-
message = exceptionResponse;
|
|
30
|
-
error = exception.message;
|
|
31
|
-
} else if (typeof exceptionResponse === 'object') {
|
|
32
|
-
const resp = exceptionResponse as Record<string, unknown>;
|
|
33
|
-
message =
|
|
34
|
-
typeof resp.message === 'string'
|
|
35
|
-
? resp.message
|
|
36
|
-
: Array.isArray(resp.message)
|
|
37
|
-
? resp.message.join('; ')
|
|
38
|
-
: exception.message;
|
|
39
|
-
error = (resp.error as string) ?? exception.message;
|
|
40
|
-
}
|
|
41
|
-
} else if (exception instanceof Error) {
|
|
42
|
-
message = exception.message;
|
|
43
|
-
error = exception.name;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (status >= 500) {
|
|
47
|
-
this.logger.error(
|
|
48
|
-
`${request.method} ${request.url} → ${status}`,
|
|
49
|
-
exception instanceof Error ? exception.stack : undefined,
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
response.status(status).json({
|
|
54
|
-
statusCode: status,
|
|
55
|
-
message,
|
|
56
|
-
error,
|
|
57
|
-
timestamp: new Date().toISOString(),
|
|
58
|
-
path: request.url,
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ExceptionFilter,
|
|
3
|
+
Catch,
|
|
4
|
+
ArgumentsHost,
|
|
5
|
+
HttpException,
|
|
6
|
+
HttpStatus,
|
|
7
|
+
Logger,
|
|
8
|
+
} from '@nestjs/common';
|
|
9
|
+
import { Request, Response } from 'express';
|
|
10
|
+
|
|
11
|
+
@Catch()
|
|
12
|
+
export class GlobalExceptionFilter implements ExceptionFilter {
|
|
13
|
+
private readonly logger = new Logger(GlobalExceptionFilter.name);
|
|
14
|
+
|
|
15
|
+
catch(exception: unknown, host: ArgumentsHost): void {
|
|
16
|
+
const ctx = host.switchToHttp();
|
|
17
|
+
const response = ctx.getResponse<Response>();
|
|
18
|
+
const request = ctx.getRequest<Request>();
|
|
19
|
+
|
|
20
|
+
let status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
21
|
+
let message = 'Error interno del servidor';
|
|
22
|
+
let error = 'Internal Server Error';
|
|
23
|
+
|
|
24
|
+
if (exception instanceof HttpException) {
|
|
25
|
+
status = exception.getStatus();
|
|
26
|
+
const exceptionResponse = exception.getResponse();
|
|
27
|
+
|
|
28
|
+
if (typeof exceptionResponse === 'string') {
|
|
29
|
+
message = exceptionResponse;
|
|
30
|
+
error = exception.message;
|
|
31
|
+
} else if (typeof exceptionResponse === 'object') {
|
|
32
|
+
const resp = exceptionResponse as Record<string, unknown>;
|
|
33
|
+
message =
|
|
34
|
+
typeof resp.message === 'string'
|
|
35
|
+
? resp.message
|
|
36
|
+
: Array.isArray(resp.message)
|
|
37
|
+
? resp.message.join('; ')
|
|
38
|
+
: exception.message;
|
|
39
|
+
error = (resp.error as string) ?? exception.message;
|
|
40
|
+
}
|
|
41
|
+
} else if (exception instanceof Error) {
|
|
42
|
+
message = exception.message;
|
|
43
|
+
error = exception.name;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (status >= 500) {
|
|
47
|
+
this.logger.error(
|
|
48
|
+
`${request.method} ${request.url} → ${status}`,
|
|
49
|
+
exception instanceof Error ? exception.stack : undefined,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
response.status(status).json({
|
|
54
|
+
statusCode: status,
|
|
55
|
+
message,
|
|
56
|
+
error,
|
|
57
|
+
timestamp: new Date().toISOString(),
|
|
58
|
+
path: request.url,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Injectable } from '@nestjs/common';
|
|
2
|
-
import { AuthGuard } from '@nestjs/passport';
|
|
3
|
-
|
|
4
|
-
@Injectable()
|
|
5
|
-
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { AuthGuard } from '@nestjs/passport';
|
|
3
|
+
|
|
4
|
+
@Injectable()
|
|
5
|
+
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Injectable,
|
|
3
|
-
NestInterceptor,
|
|
4
|
-
ExecutionContext,
|
|
5
|
-
CallHandler,
|
|
6
|
-
BadRequestException,
|
|
7
|
-
} from '@nestjs/common';
|
|
8
|
-
import { Observable } from 'rxjs';
|
|
9
|
-
import { Request } from 'express';
|
|
10
|
-
|
|
11
|
-
export interface TenantRequest extends Request {
|
|
12
|
-
workspaceId?: string;
|
|
13
|
-
user?: Record<string, unknown>;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
@Injectable()
|
|
17
|
-
export class WorkspaceTenantInterceptor implements NestInterceptor {
|
|
18
|
-
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
19
|
-
const request = context.switchToHttp().getRequest<TenantRequest>();
|
|
20
|
-
|
|
21
|
-
const headerWorkspaceId = request.headers['x-workspace-id'];
|
|
22
|
-
if (headerWorkspaceId && typeof headerWorkspaceId === 'string') {
|
|
23
|
-
request.workspaceId = headerWorkspaceId;
|
|
24
|
-
return next.handle();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const workspaceIdFromUser =
|
|
28
|
-
typeof request.user?.workspaceId === 'string'
|
|
29
|
-
? request.user.workspaceId
|
|
30
|
-
: undefined;
|
|
31
|
-
if (workspaceIdFromUser) {
|
|
32
|
-
request.workspaceId = workspaceIdFromUser;
|
|
33
|
-
return next.handle();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const apiPrefix = process.env.API_PREFIX ?? 'api';
|
|
37
|
-
if (request.path.startsWith(`/${apiPrefix}/auth/`)) {
|
|
38
|
-
return next.handle();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
throw new BadRequestException(
|
|
42
|
-
'Se requiere workspaceId en header x-workspace-id o token JWT',
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Injectable,
|
|
3
|
+
NestInterceptor,
|
|
4
|
+
ExecutionContext,
|
|
5
|
+
CallHandler,
|
|
6
|
+
BadRequestException,
|
|
7
|
+
} from '@nestjs/common';
|
|
8
|
+
import { Observable } from 'rxjs';
|
|
9
|
+
import { Request } from 'express';
|
|
10
|
+
|
|
11
|
+
export interface TenantRequest extends Request {
|
|
12
|
+
workspaceId?: string;
|
|
13
|
+
user?: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Injectable()
|
|
17
|
+
export class WorkspaceTenantInterceptor implements NestInterceptor {
|
|
18
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
19
|
+
const request = context.switchToHttp().getRequest<TenantRequest>();
|
|
20
|
+
|
|
21
|
+
const headerWorkspaceId = request.headers['x-workspace-id'];
|
|
22
|
+
if (headerWorkspaceId && typeof headerWorkspaceId === 'string') {
|
|
23
|
+
request.workspaceId = headerWorkspaceId;
|
|
24
|
+
return next.handle();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const workspaceIdFromUser =
|
|
28
|
+
typeof request.user?.workspaceId === 'string'
|
|
29
|
+
? request.user.workspaceId
|
|
30
|
+
: undefined;
|
|
31
|
+
if (workspaceIdFromUser) {
|
|
32
|
+
request.workspaceId = workspaceIdFromUser;
|
|
33
|
+
return next.handle();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const apiPrefix = process.env.API_PREFIX ?? 'api';
|
|
37
|
+
if (request.path.startsWith(`/${apiPrefix}/auth/`)) {
|
|
38
|
+
return next.handle();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
throw new BadRequestException(
|
|
42
|
+
'Se requiere workspaceId en header x-workspace-id o token JWT',
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
import { NestFactory } from '@nestjs/core';
|
|
2
|
-
import { ValidationPipe } from '@nestjs/common';
|
|
3
|
-
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
4
|
-
import cookieParser from 'cookie-parser';
|
|
5
|
-
import { AppModule } from './app.module';
|
|
6
|
-
import { WorkspaceTenantInterceptor } from './common/interceptors/workspace-tenant.interceptor';
|
|
7
|
-
|
|
8
|
-
async function bootstrap() {
|
|
9
|
-
const app = await NestFactory.create(AppModule);
|
|
10
|
-
|
|
11
|
-
const apiPrefix = process.env.API_PREFIX ?? 'api';
|
|
12
|
-
app.setGlobalPrefix(apiPrefix);
|
|
13
|
-
|
|
14
|
-
app.use(cookieParser(process.env.COOKIE_SECRET));
|
|
15
|
-
|
|
16
|
-
app.enableCors({
|
|
17
|
-
origin: process.env.CORS_ORIGINS?.split(',') ?? ['http://localhost:5173'],
|
|
18
|
-
credentials: true,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
app.useGlobalPipes(
|
|
22
|
-
new ValidationPipe({
|
|
23
|
-
whitelist: true,
|
|
24
|
-
forbidNonWhitelisted: true,
|
|
25
|
-
transform: true,
|
|
26
|
-
transformOptions: {
|
|
27
|
-
enableImplicitConversion: true,
|
|
28
|
-
},
|
|
29
|
-
}),
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
app.useGlobalInterceptors(new WorkspaceTenantInterceptor());
|
|
33
|
-
|
|
34
|
-
if (process.env.SWAGGER_ENABLED === 'true') {
|
|
35
|
-
const config = new DocumentBuilder()
|
|
36
|
-
.setTitle('create-saas-ar API')
|
|
37
|
-
.setDescription('Backend SaaS multi-tenant')
|
|
38
|
-
.setVersion('1.0')
|
|
39
|
-
.addCookieAuth('access_token')
|
|
40
|
-
.build();
|
|
41
|
-
|
|
42
|
-
const document = SwaggerModule.createDocument(app, config);
|
|
43
|
-
SwaggerModule.setup(`${apiPrefix}/docs`, app, document);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const port = process.env.PORT ?? 3000;
|
|
47
|
-
await app.listen(port);
|
|
48
|
-
console.log(`🚀 Servidor corriendo en http://localhost:${port}/${apiPrefix}`);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
void bootstrap();
|
|
1
|
+
import { NestFactory } from '@nestjs/core';
|
|
2
|
+
import { ValidationPipe } from '@nestjs/common';
|
|
3
|
+
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
4
|
+
import cookieParser from 'cookie-parser';
|
|
5
|
+
import { AppModule } from './app.module';
|
|
6
|
+
import { WorkspaceTenantInterceptor } from './common/interceptors/workspace-tenant.interceptor';
|
|
7
|
+
|
|
8
|
+
async function bootstrap() {
|
|
9
|
+
const app = await NestFactory.create(AppModule);
|
|
10
|
+
|
|
11
|
+
const apiPrefix = process.env.API_PREFIX ?? 'api';
|
|
12
|
+
app.setGlobalPrefix(apiPrefix);
|
|
13
|
+
|
|
14
|
+
app.use(cookieParser(process.env.COOKIE_SECRET));
|
|
15
|
+
|
|
16
|
+
app.enableCors({
|
|
17
|
+
origin: process.env.CORS_ORIGINS?.split(',') ?? ['http://localhost:5173'],
|
|
18
|
+
credentials: true,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
app.useGlobalPipes(
|
|
22
|
+
new ValidationPipe({
|
|
23
|
+
whitelist: true,
|
|
24
|
+
forbidNonWhitelisted: true,
|
|
25
|
+
transform: true,
|
|
26
|
+
transformOptions: {
|
|
27
|
+
enableImplicitConversion: true,
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
app.useGlobalInterceptors(new WorkspaceTenantInterceptor());
|
|
33
|
+
|
|
34
|
+
if (process.env.SWAGGER_ENABLED === 'true') {
|
|
35
|
+
const config = new DocumentBuilder()
|
|
36
|
+
.setTitle('create-saas-ar API')
|
|
37
|
+
.setDescription('Backend SaaS multi-tenant')
|
|
38
|
+
.setVersion('1.0')
|
|
39
|
+
.addCookieAuth('access_token')
|
|
40
|
+
.build();
|
|
41
|
+
|
|
42
|
+
const document = SwaggerModule.createDocument(app, config);
|
|
43
|
+
SwaggerModule.setup(`${apiPrefix}/docs`, app, document);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const port = process.env.PORT ?? 3000;
|
|
47
|
+
await app.listen(port);
|
|
48
|
+
console.log(`🚀 Servidor corriendo en http://localhost:${port}/${apiPrefix}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
void bootstrap();
|
|
@@ -1,158 +1,158 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Body,
|
|
3
|
-
Controller,
|
|
4
|
-
Get,
|
|
5
|
-
HttpCode,
|
|
6
|
-
HttpStatus,
|
|
7
|
-
Post,
|
|
8
|
-
Query,
|
|
9
|
-
Res,
|
|
10
|
-
UnauthorizedException,
|
|
11
|
-
UseGuards,
|
|
12
|
-
} from '@nestjs/common';
|
|
13
|
-
import { ConfigService } from '@nestjs/config';
|
|
14
|
-
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
15
|
-
import type { Response } from 'express';
|
|
16
|
-
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
|
17
|
-
import type { TokenPayload } from '../../common/decorators/current-user.decorator';
|
|
18
|
-
import { Cookie } from '../../common/decorators/cookie.decorator';
|
|
19
|
-
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
|
20
|
-
import { AuthService } from './auth.service';
|
|
21
|
-
import { ForgotPasswordDto } from './dto/forgot-password.dto';
|
|
22
|
-
import { LoginDto } from './dto/login.dto';
|
|
23
|
-
import { RefreshTokenDto } from './dto/refresh-token.dto';
|
|
24
|
-
import { RegisterDto } from './dto/register.dto';
|
|
25
|
-
import { ResetPasswordDto } from './dto/reset-password.dto';
|
|
26
|
-
import { VerifyEmailDto } from './dto/verify-email.dto';
|
|
27
|
-
|
|
28
|
-
@ApiTags('Auth')
|
|
29
|
-
@Controller('auth')
|
|
30
|
-
export class AuthController {
|
|
31
|
-
private readonly refreshCookiePath: string;
|
|
32
|
-
|
|
33
|
-
constructor(
|
|
34
|
-
private readonly authService: AuthService,
|
|
35
|
-
private readonly configService: ConfigService,
|
|
36
|
-
) {
|
|
37
|
-
const prefix = this.configService.get<string>('API_PREFIX', 'api');
|
|
38
|
-
this.refreshCookiePath = `/${prefix}/auth/refresh`;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
@Post('register')
|
|
42
|
-
@HttpCode(HttpStatus.CREATED)
|
|
43
|
-
@ApiOperation({ summary: 'Registrar nuevo usuario y workspace' })
|
|
44
|
-
register(@Body() dto: RegisterDto) {
|
|
45
|
-
return this.authService.register(dto);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
@Get('verify-email')
|
|
49
|
-
@ApiOperation({ summary: 'Verificar email con token' })
|
|
50
|
-
verifyEmail(@Query() dto: VerifyEmailDto) {
|
|
51
|
-
return this.authService.verifyEmail(dto.token);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
@Post('login')
|
|
55
|
-
@HttpCode(HttpStatus.OK)
|
|
56
|
-
@ApiOperation({ summary: 'Iniciar sesión' })
|
|
57
|
-
async login(
|
|
58
|
-
@Body() dto: LoginDto,
|
|
59
|
-
@Res({ passthrough: true }) res: Response,
|
|
60
|
-
) {
|
|
61
|
-
const { user, accessToken, refreshToken } =
|
|
62
|
-
await this.authService.login(dto);
|
|
63
|
-
this.setTokenCookies(res, accessToken, refreshToken);
|
|
64
|
-
return user;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
@Post('refresh')
|
|
68
|
-
@HttpCode(HttpStatus.OK)
|
|
69
|
-
@ApiOperation({ summary: 'Renovar access token usando el refresh token' })
|
|
70
|
-
async refresh(
|
|
71
|
-
@Cookie('refresh_token') cookieToken: string | undefined,
|
|
72
|
-
@Body() dto: RefreshTokenDto,
|
|
73
|
-
@Res({ passthrough: true }) res: Response,
|
|
74
|
-
) {
|
|
75
|
-
const token = cookieToken ?? dto.refreshToken;
|
|
76
|
-
if (!token) {
|
|
77
|
-
throw new UnauthorizedException('Refresh token no encontrado.');
|
|
78
|
-
}
|
|
79
|
-
const tokens = await this.authService.refresh(token);
|
|
80
|
-
this.setTokenCookies(res, tokens.accessToken, tokens.refreshToken);
|
|
81
|
-
return { message: 'Token refreshed' };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
@Post('logout')
|
|
85
|
-
@HttpCode(HttpStatus.OK)
|
|
86
|
-
@UseGuards(JwtAuthGuard)
|
|
87
|
-
@ApiOperation({ summary: 'Cerrar sesión' })
|
|
88
|
-
async logout(
|
|
89
|
-
@CurrentUser() user: TokenPayload,
|
|
90
|
-
@Res({ passthrough: true }) res: Response,
|
|
91
|
-
) {
|
|
92
|
-
await this.authService.logout(user.userId);
|
|
93
|
-
this.clearTokenCookies(res);
|
|
94
|
-
return { message: 'Logged out' };
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
@Post('forgot-password')
|
|
98
|
-
@HttpCode(HttpStatus.OK)
|
|
99
|
-
@ApiOperation({ summary: 'Solicitar restablecimiento de contraseña' })
|
|
100
|
-
forgotPassword(@Body() dto: ForgotPasswordDto) {
|
|
101
|
-
return this.authService.forgotPassword(dto);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
@Post('reset-password')
|
|
105
|
-
@HttpCode(HttpStatus.OK)
|
|
106
|
-
@ApiOperation({ summary: 'Restablecer contraseña con token del email' })
|
|
107
|
-
resetPassword(@Body() dto: ResetPasswordDto) {
|
|
108
|
-
return this.authService.resetPassword(dto);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
@Get('me')
|
|
112
|
-
@UseGuards(JwtAuthGuard)
|
|
113
|
-
@ApiOperation({ summary: 'Obtener perfil del usuario autenticado' })
|
|
114
|
-
getMe(@CurrentUser() user: TokenPayload) {
|
|
115
|
-
return this.authService.getMe(user.userId);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// ─── Private helpers ────────────────────────────────────────────────────────
|
|
119
|
-
|
|
120
|
-
private setTokenCookies(
|
|
121
|
-
res: Response,
|
|
122
|
-
accessToken: string,
|
|
123
|
-
refreshToken: string,
|
|
124
|
-
): void {
|
|
125
|
-
const isProd = this.configService.get<string>('NODE_ENV') === 'production';
|
|
126
|
-
const base = {
|
|
127
|
-
httpOnly: true,
|
|
128
|
-
secure: isProd,
|
|
129
|
-
sameSite: 'lax' as const,
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
res.cookie('access_token', accessToken, {
|
|
133
|
-
...base,
|
|
134
|
-
maxAge: 15 * 60 * 1000,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
res.cookie('refresh_token', refreshToken, {
|
|
138
|
-
...base,
|
|
139
|
-
path: this.refreshCookiePath,
|
|
140
|
-
maxAge: 7 * 24 * 60 * 60 * 1000,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
private clearTokenCookies(res: Response): void {
|
|
145
|
-
const isProd = this.configService.get<string>('NODE_ENV') === 'production';
|
|
146
|
-
const base = {
|
|
147
|
-
httpOnly: true,
|
|
148
|
-
secure: isProd,
|
|
149
|
-
sameSite: 'lax' as const,
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
res.clearCookie('access_token', base);
|
|
153
|
-
res.clearCookie('refresh_token', {
|
|
154
|
-
...base,
|
|
155
|
-
path: this.refreshCookiePath,
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
Body,
|
|
3
|
+
Controller,
|
|
4
|
+
Get,
|
|
5
|
+
HttpCode,
|
|
6
|
+
HttpStatus,
|
|
7
|
+
Post,
|
|
8
|
+
Query,
|
|
9
|
+
Res,
|
|
10
|
+
UnauthorizedException,
|
|
11
|
+
UseGuards,
|
|
12
|
+
} from '@nestjs/common';
|
|
13
|
+
import { ConfigService } from '@nestjs/config';
|
|
14
|
+
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
15
|
+
import type { Response } from 'express';
|
|
16
|
+
import { CurrentUser } from '../../common/decorators/current-user.decorator';
|
|
17
|
+
import type { TokenPayload } from '../../common/decorators/current-user.decorator';
|
|
18
|
+
import { Cookie } from '../../common/decorators/cookie.decorator';
|
|
19
|
+
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
|
|
20
|
+
import { AuthService } from './auth.service';
|
|
21
|
+
import { ForgotPasswordDto } from './dto/forgot-password.dto';
|
|
22
|
+
import { LoginDto } from './dto/login.dto';
|
|
23
|
+
import { RefreshTokenDto } from './dto/refresh-token.dto';
|
|
24
|
+
import { RegisterDto } from './dto/register.dto';
|
|
25
|
+
import { ResetPasswordDto } from './dto/reset-password.dto';
|
|
26
|
+
import { VerifyEmailDto } from './dto/verify-email.dto';
|
|
27
|
+
|
|
28
|
+
@ApiTags('Auth')
|
|
29
|
+
@Controller('auth')
|
|
30
|
+
export class AuthController {
|
|
31
|
+
private readonly refreshCookiePath: string;
|
|
32
|
+
|
|
33
|
+
constructor(
|
|
34
|
+
private readonly authService: AuthService,
|
|
35
|
+
private readonly configService: ConfigService,
|
|
36
|
+
) {
|
|
37
|
+
const prefix = this.configService.get<string>('API_PREFIX', 'api');
|
|
38
|
+
this.refreshCookiePath = `/${prefix}/auth/refresh`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@Post('register')
|
|
42
|
+
@HttpCode(HttpStatus.CREATED)
|
|
43
|
+
@ApiOperation({ summary: 'Registrar nuevo usuario y workspace' })
|
|
44
|
+
register(@Body() dto: RegisterDto) {
|
|
45
|
+
return this.authService.register(dto);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@Get('verify-email')
|
|
49
|
+
@ApiOperation({ summary: 'Verificar email con token' })
|
|
50
|
+
verifyEmail(@Query() dto: VerifyEmailDto) {
|
|
51
|
+
return this.authService.verifyEmail(dto.token);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
@Post('login')
|
|
55
|
+
@HttpCode(HttpStatus.OK)
|
|
56
|
+
@ApiOperation({ summary: 'Iniciar sesión' })
|
|
57
|
+
async login(
|
|
58
|
+
@Body() dto: LoginDto,
|
|
59
|
+
@Res({ passthrough: true }) res: Response,
|
|
60
|
+
) {
|
|
61
|
+
const { user, accessToken, refreshToken } =
|
|
62
|
+
await this.authService.login(dto);
|
|
63
|
+
this.setTokenCookies(res, accessToken, refreshToken);
|
|
64
|
+
return user;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@Post('refresh')
|
|
68
|
+
@HttpCode(HttpStatus.OK)
|
|
69
|
+
@ApiOperation({ summary: 'Renovar access token usando el refresh token' })
|
|
70
|
+
async refresh(
|
|
71
|
+
@Cookie('refresh_token') cookieToken: string | undefined,
|
|
72
|
+
@Body() dto: RefreshTokenDto,
|
|
73
|
+
@Res({ passthrough: true }) res: Response,
|
|
74
|
+
) {
|
|
75
|
+
const token = cookieToken ?? dto.refreshToken;
|
|
76
|
+
if (!token) {
|
|
77
|
+
throw new UnauthorizedException('Refresh token no encontrado.');
|
|
78
|
+
}
|
|
79
|
+
const tokens = await this.authService.refresh(token);
|
|
80
|
+
this.setTokenCookies(res, tokens.accessToken, tokens.refreshToken);
|
|
81
|
+
return { message: 'Token refreshed' };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@Post('logout')
|
|
85
|
+
@HttpCode(HttpStatus.OK)
|
|
86
|
+
@UseGuards(JwtAuthGuard)
|
|
87
|
+
@ApiOperation({ summary: 'Cerrar sesión' })
|
|
88
|
+
async logout(
|
|
89
|
+
@CurrentUser() user: TokenPayload,
|
|
90
|
+
@Res({ passthrough: true }) res: Response,
|
|
91
|
+
) {
|
|
92
|
+
await this.authService.logout(user.userId);
|
|
93
|
+
this.clearTokenCookies(res);
|
|
94
|
+
return { message: 'Logged out' };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@Post('forgot-password')
|
|
98
|
+
@HttpCode(HttpStatus.OK)
|
|
99
|
+
@ApiOperation({ summary: 'Solicitar restablecimiento de contraseña' })
|
|
100
|
+
forgotPassword(@Body() dto: ForgotPasswordDto) {
|
|
101
|
+
return this.authService.forgotPassword(dto);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@Post('reset-password')
|
|
105
|
+
@HttpCode(HttpStatus.OK)
|
|
106
|
+
@ApiOperation({ summary: 'Restablecer contraseña con token del email' })
|
|
107
|
+
resetPassword(@Body() dto: ResetPasswordDto) {
|
|
108
|
+
return this.authService.resetPassword(dto);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
@Get('me')
|
|
112
|
+
@UseGuards(JwtAuthGuard)
|
|
113
|
+
@ApiOperation({ summary: 'Obtener perfil del usuario autenticado' })
|
|
114
|
+
getMe(@CurrentUser() user: TokenPayload) {
|
|
115
|
+
return this.authService.getMe(user.userId);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// ─── Private helpers ────────────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
private setTokenCookies(
|
|
121
|
+
res: Response,
|
|
122
|
+
accessToken: string,
|
|
123
|
+
refreshToken: string,
|
|
124
|
+
): void {
|
|
125
|
+
const isProd = this.configService.get<string>('NODE_ENV') === 'production';
|
|
126
|
+
const base = {
|
|
127
|
+
httpOnly: true,
|
|
128
|
+
secure: isProd,
|
|
129
|
+
sameSite: 'lax' as const,
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
res.cookie('access_token', accessToken, {
|
|
133
|
+
...base,
|
|
134
|
+
maxAge: 15 * 60 * 1000,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
res.cookie('refresh_token', refreshToken, {
|
|
138
|
+
...base,
|
|
139
|
+
path: this.refreshCookiePath,
|
|
140
|
+
maxAge: 7 * 24 * 60 * 60 * 1000,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private clearTokenCookies(res: Response): void {
|
|
145
|
+
const isProd = this.configService.get<string>('NODE_ENV') === 'production';
|
|
146
|
+
const base = {
|
|
147
|
+
httpOnly: true,
|
|
148
|
+
secure: isProd,
|
|
149
|
+
sameSite: 'lax' as const,
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
res.clearCookie('access_token', base);
|
|
153
|
+
res.clearCookie('refresh_token', {
|
|
154
|
+
...base,
|
|
155
|
+
path: this.refreshCookiePath,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|