Skip to content

Commit 1cceb68

Browse files
committed
Add isOnboarded field to JWT token
1 parent 7bcbab4 commit 1cceb68

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

backend/auth-service/src/app.service.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export class AppService {
5656
const tokens = await this.generateTokens({
5757
id: userId,
5858
email: newUser.email,
59+
isOnboarded: newUser.isOnboarded,
5960
roles: newUser.roles,
6061
});
6162
await this.updateRefreshToken({
@@ -93,6 +94,7 @@ export class AppService {
9394
const tokens = await this.generateTokens({
9495
id: userId,
9596
email: user.email,
97+
isOnboarded: user.isOnboarded,
9698
roles: user.roles,
9799
});
98100
await this.updateRefreshToken({
@@ -141,6 +143,7 @@ export class AppService {
141143
const tokens = await this.generateTokens({
142144
id: id,
143145
email: user.email,
146+
isOnboarded: user.isOnboarded,
144147
roles: user.roles,
145148
});
146149
await this.updateRefreshToken({ id, refreshToken: tokens.refresh_token });
@@ -286,25 +289,23 @@ export class AppService {
286289

287290
// Could include other fields like roles in the future
288291
private async generateTokens(payload: TokenPayload): Promise<Token> {
289-
const { id, email, roles } = payload;
292+
const { id, ...rest } = payload;
290293

291294
const [accessToken, refreshToken] = await Promise.all([
292295
this.jwtService.signAsync(
293-
{
294-
sub: id,
295-
email,
296-
roles,
297-
},
298-
{
299-
secret: process.env.JWT_SECRET,
300-
expiresIn: '15m', // 15 minute
296+
{
297+
sub: id,
298+
...rest,
299+
},
300+
{
301+
secret: process.env.JWT_SECRET,
302+
expiresIn: '15m', // 15 minutes
301303
},
302304
),
303305
this.jwtService.signAsync(
304306
{
305307
sub: id,
306-
email,
307-
roles,
308+
...rest,
308309
},
309310
{
310311
secret: process.env.JWT_REFRESH_SECRET,
@@ -365,6 +366,7 @@ export class AppService {
365366
const jwtTokens = await this.generateTokens({
366367
id: user._id.toString(),
367368
email: user.email,
369+
isOnboarded: user.isOnboarded,
368370
roles: user.roles,
369371
});
370372

@@ -467,6 +469,7 @@ export class AppService {
467469
const jwtTokens = await this.generateTokens({
468470
id: user._id.toString(),
469471
email: user.email,
472+
isOnboarded: user.isOnboarded,
470473
roles: user.roles,
471474
});
472475

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface TokenPayload {
22
id: string;
33
email: string;
4+
isOnboarded: boolean;
45
roles: string[];
56
}

backend/gateway-service/src/modules/auth/auth.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { AuthDto, ResetPasswordDto, ResetPasswordRequestDto } from './dto';
2020
import { Token } from './interfaces';
2121
import { ClientProxy } from '@nestjs/microservices';
22-
import { first, firstValueFrom } from 'rxjs';
22+
import { firstValueFrom } from 'rxjs';
2323
import { RtAuthGuard } from '../../common/guards';
2424
import { GetCurrentUserId } from 'src/common/decorators/get-current-user-id.decorator';
2525
import { GetCurrentUser, Public } from 'src/common/decorators';

0 commit comments

Comments
 (0)