ar-saas 0.3.2 → 0.4.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.
Files changed (28) hide show
  1. package/dist/generator.js +2 -6
  2. package/package.json +1 -1
  3. package/templates/backend/.env.example +1 -1
  4. package/templates/backend/package.json +5 -2
  5. package/templates/backend/src/app.module.ts +68 -40
  6. package/templates/backend/src/common/interceptors/workspace-tenant.interceptor.ts +27 -45
  7. package/templates/backend/src/main.ts +50 -51
  8. package/templates/backend/src/modules/auth/auth.controller.ts +162 -158
  9. package/templates/backend/src/modules/auth/auth.service.ts +236 -257
  10. package/templates/backend/src/modules/auth/strategies/jwt.strategy.ts +45 -43
  11. package/templates/backend/src/modules/users/users.controller.ts +28 -0
  12. package/templates/backend/src/modules/users/users.module.ts +16 -14
  13. package/templates/backend/src/modules/users/users.repository.ts +57 -51
  14. package/templates/backend/src/modules/users/users.service.ts +130 -104
  15. package/templates/backend/src/modules/workspaces/workspaces.repository.ts +38 -34
  16. package/templates/backend/src/modules/workspaces/workspaces.service.ts +51 -42
  17. package/templates/frontend/package.json +2 -5
  18. package/templates/frontend/pnpm-workspace.yaml +2 -2
  19. package/templates/frontend/src/app/(auth)/layout.tsx +29 -28
  20. package/templates/frontend/src/app/(dashboard)/billing/page.tsx +111 -111
  21. package/templates/frontend/src/app/(dashboard)/profile/page.tsx +241 -226
  22. package/templates/frontend/src/app/(dashboard)/settings/page.tsx +155 -156
  23. package/templates/frontend/src/app/(dashboard)/team/page.tsx +179 -178
  24. package/templates/frontend/src/app/layout.tsx +29 -26
  25. package/templates/frontend/src/app/page.tsx +1 -1
  26. package/templates/frontend/src/app/setup/page.tsx +1 -1
  27. package/templates/frontend/src/components/dashboard/header.tsx +5 -3
  28. package/templates/frontend/src/config/site.ts +1 -1
@@ -1,158 +1,162 @@
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 { Throttle } from '@nestjs/throttler';
15
+ import { ApiOperation, ApiTags } from '@nestjs/swagger';
16
+ import type { Response } from 'express';
17
+ import { CurrentUser } from '../../common/decorators/current-user.decorator';
18
+ import type { TokenPayload } from '../../common/decorators/current-user.decorator';
19
+ import { Cookie } from '../../common/decorators/cookie.decorator';
20
+ import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
21
+ import { AuthService } from './auth.service';
22
+ import { ForgotPasswordDto } from './dto/forgot-password.dto';
23
+ import { LoginDto } from './dto/login.dto';
24
+ import { RefreshTokenDto } from './dto/refresh-token.dto';
25
+ import { RegisterDto } from './dto/register.dto';
26
+ import { ResetPasswordDto } from './dto/reset-password.dto';
27
+ import { VerifyEmailDto } from './dto/verify-email.dto';
28
+
29
+ @ApiTags('Auth')
30
+ @Controller('auth')
31
+ export class AuthController {
32
+ private readonly refreshCookiePath: string;
33
+
34
+ constructor(
35
+ private readonly authService: AuthService,
36
+ private readonly configService: ConfigService,
37
+ ) {
38
+ const prefix = this.configService.get<string>('API_PREFIX', 'api');
39
+ this.refreshCookiePath = `/${prefix}/auth/refresh`;
40
+ }
41
+
42
+ @Post('register')
43
+ @HttpCode(HttpStatus.CREATED)
44
+ @Throttle({ default: { limit: 5, ttl: 60000 } })
45
+ @ApiOperation({ summary: 'Registrar nuevo usuario y workspace' })
46
+ register(@Body() dto: RegisterDto) {
47
+ return this.authService.register(dto);
48
+ }
49
+
50
+ @Get('verify-email')
51
+ @ApiOperation({ summary: 'Verificar email con token' })
52
+ verifyEmail(@Query() dto: VerifyEmailDto) {
53
+ return this.authService.verifyEmail(dto.token);
54
+ }
55
+
56
+ @Post('login')
57
+ @HttpCode(HttpStatus.OK)
58
+ @Throttle({ default: { limit: 5, ttl: 60000 } })
59
+ @ApiOperation({ summary: 'Iniciar sesión' })
60
+ async login(
61
+ @Body() dto: LoginDto,
62
+ @Res({ passthrough: true }) res: Response,
63
+ ) {
64
+ const { user, accessToken, refreshToken } =
65
+ await this.authService.login(dto);
66
+ this.setTokenCookies(res, accessToken, refreshToken);
67
+ return user;
68
+ }
69
+
70
+ @Post('refresh')
71
+ @HttpCode(HttpStatus.OK)
72
+ @Throttle({ default: { limit: 10, ttl: 60000 } })
73
+ @ApiOperation({ summary: 'Renovar access token usando el refresh token' })
74
+ async refresh(
75
+ @Cookie('refresh_token') cookieToken: string | undefined,
76
+ @Body() dto: RefreshTokenDto,
77
+ @Res({ passthrough: true }) res: Response,
78
+ ) {
79
+ const token = cookieToken ?? dto.refreshToken;
80
+ if (!token) {
81
+ throw new UnauthorizedException('Refresh token no encontrado.');
82
+ }
83
+ const tokens = await this.authService.refresh(token);
84
+ this.setTokenCookies(res, tokens.accessToken, tokens.refreshToken);
85
+ return { message: 'Token refreshed' };
86
+ }
87
+
88
+ @Post('logout')
89
+ @HttpCode(HttpStatus.OK)
90
+ @UseGuards(JwtAuthGuard)
91
+ @ApiOperation({ summary: 'Cerrar sesión' })
92
+ async logout(
93
+ @CurrentUser() user: TokenPayload,
94
+ @Res({ passthrough: true }) res: Response,
95
+ ) {
96
+ await this.authService.logout(user.userId);
97
+ this.clearTokenCookies(res);
98
+ return { message: 'Logged out' };
99
+ }
100
+
101
+ @Post('forgot-password')
102
+ @HttpCode(HttpStatus.OK)
103
+ @Throttle({ default: { limit: 3, ttl: 60000 } })
104
+ @ApiOperation({ summary: 'Solicitar restablecimiento de contraseña' })
105
+ forgotPassword(@Body() dto: ForgotPasswordDto) {
106
+ return this.authService.forgotPassword(dto);
107
+ }
108
+
109
+ @Post('reset-password')
110
+ @HttpCode(HttpStatus.OK)
111
+ @Throttle({ default: { limit: 3, ttl: 60000 } })
112
+ @ApiOperation({ summary: 'Restablecer contraseña con token del email' })
113
+ resetPassword(@Body() dto: ResetPasswordDto) {
114
+ return this.authService.resetPassword(dto);
115
+ }
116
+
117
+ @Get('me')
118
+ @UseGuards(JwtAuthGuard)
119
+ @ApiOperation({ summary: 'Obtener perfil del usuario autenticado' })
120
+ getMe(@CurrentUser() user: TokenPayload) {
121
+ return this.authService.getMe(user.userId);
122
+ }
123
+
124
+ private setTokenCookies(
125
+ res: Response,
126
+ accessToken: string,
127
+ refreshToken: string,
128
+ ): void {
129
+ const isProd = this.configService.get<string>('NODE_ENV') === 'production';
130
+ const base = {
131
+ httpOnly: true,
132
+ secure: isProd,
133
+ sameSite: 'strict' as const,
134
+ };
135
+
136
+ res.cookie('access_token', accessToken, {
137
+ ...base,
138
+ maxAge: 15 * 60 * 1000,
139
+ });
140
+
141
+ res.cookie('refresh_token', refreshToken, {
142
+ ...base,
143
+ path: this.refreshCookiePath,
144
+ maxAge: 7 * 24 * 60 * 60 * 1000,
145
+ });
146
+ }
147
+
148
+ private clearTokenCookies(res: Response): void {
149
+ const isProd = this.configService.get<string>('NODE_ENV') === 'production';
150
+ const base = {
151
+ httpOnly: true,
152
+ secure: isProd,
153
+ sameSite: 'strict' as const,
154
+ };
155
+
156
+ res.clearCookie('access_token', base);
157
+ res.clearCookie('refresh_token', {
158
+ ...base,
159
+ path: this.refreshCookiePath,
160
+ });
161
+ }
162
+ }