Skip to content

Commit fa88084

Browse files
committed
fix(auth): enhance refresh token cookie handling with environment-specific settings
1 parent a2f56be commit fa88084

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

src/auth/application/controller/auth/controller.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ import { BearerAuthGuard, CookieAuthGuard } from '@shared/guards';
1212
import { AuthFacade } from '../../auth.facade';
1313
import { getDeviceMeta } from '@core/auth/infrastructure/utils/get-device-meta';
1414
import { ApiBaseController } from '@shared/decorators';
15+
import { ConfigService } from '@nestjs/config';
1516

1617
@ApiBaseController('auth', 'Auth')
1718
export class AuthController {
18-
constructor(private readonly facade: AuthFacade) {}
19+
constructor(
20+
private readonly facade: AuthFacade,
21+
private cfg: ConfigService,
22+
) {
23+
this.isProduction = this.cfg.get('NODE_ENV') === 'production';
24+
this.domain = this.cfg.get('DOMAIN');
25+
}
26+
27+
private readonly isProduction: boolean;
28+
private readonly domain: string;
1929

2030
@Post('sign-up')
2131
@PostRegisterSwagger()
@@ -35,12 +45,7 @@ export class AuthController {
3545
const meta = getDeviceMeta(req);
3646
const { tokens, ...response } = await this.facade.verifySignUp(dto, meta);
3747

38-
res.setCookie('refresh', tokens.refresh, {
39-
httpOnly: true,
40-
secure: false,
41-
path: '/',
42-
sameSite: 'lax',
43-
});
48+
this.setRefreshCookie(res, tokens.refresh);
4449

4550
return { ...response, token: tokens.access };
4651
}
@@ -55,12 +60,7 @@ export class AuthController {
5560
const meta = getDeviceMeta(req);
5661
const { tokens, ...response } = await this.facade.signIn(dto, meta);
5762

58-
res.setCookie('refresh', tokens.refresh, {
59-
httpOnly: true,
60-
secure: false,
61-
path: '/',
62-
sameSite: 'lax',
63-
});
63+
this.setRefreshCookie(res, tokens.refresh);
6464

6565
return { ...response, token: tokens.access };
6666
}
@@ -87,13 +87,18 @@ export class AuthController {
8787
const session = req.cookies?.['refresh'];
8888
const { tokens, ...response } = await this.facade.refreshTokens(session, meta);
8989

90-
res.setCookie('refresh', tokens.refresh, {
90+
this.setRefreshCookie(res, tokens.refresh);
91+
92+
return { token: tokens.access, ...response };
93+
}
94+
95+
private setRefreshCookie(res: FastifyReply, refreshToken: string) {
96+
res.setCookie('refresh', refreshToken, {
9197
httpOnly: true,
92-
secure: false,
98+
secure: this.isProduction,
9399
path: '/',
94100
sameSite: 'lax',
101+
domain: `*.${this.domain}`,
95102
});
96-
97-
return { token: tokens.access, ...response };
98103
}
99104
}

0 commit comments

Comments
 (0)