Skip to content

Commit 64e8949

Browse files
Merge pull request #3371 from AdityaAnuragi/issue-3370-remove-redundant-jwt-secret-key
docs(auth): remove redundant jwt secret in authentication code samples
2 parents 99fe6fa + 3fda9cf commit 64e8949

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

content/security/authentication.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ export class AuthService {
242242
}
243243
const payload = { sub: user.userId, username: user.username };
244244
return {
245+
// 💡 Here the JWT secret key that's used for signing the payload
246+
// is the key that was passsed in the JwtModule
245247
access_token: await this.jwtService.signAsync(payload),
246248
};
247249
}
@@ -266,6 +268,8 @@ export class AuthService {
266268
}
267269
const payload = { username: user.username, sub: user.userId };
268270
return {
271+
// 💡 Here the JWT secret key that's used for signing the payload
272+
// is the key that was passsed in the JwtModule
269273
access_token: await this.jwtService.signAsync(payload),
270274
};
271275
}
@@ -368,7 +372,6 @@ import {
368372
UnauthorizedException,
369373
} from '@nestjs/common';
370374
import { JwtService } from '@nestjs/jwt';
371-
import { jwtConstants } from './constants';
372375
import { Request } from 'express';
373376

374377
@Injectable()
@@ -382,12 +385,9 @@ export class AuthGuard implements CanActivate {
382385
throw new UnauthorizedException();
383386
}
384387
try {
385-
const payload = await this.jwtService.verifyAsync(
386-
token,
387-
{
388-
secret: jwtConstants.secret
389-
}
390-
);
388+
// 💡 Here the JWT secret key that's used for verifying the payload
389+
// is the key that was passsed in the JwtModule
390+
const payload = await this.jwtService.verifyAsync(token);
391391
// 💡 We're assigning the payload to the request object here
392392
// so that we can access it in our route handlers
393393
request['user'] = payload;
@@ -524,9 +524,9 @@ export class AuthGuard implements CanActivate {
524524
throw new UnauthorizedException();
525525
}
526526
try {
527-
const payload = await this.jwtService.verifyAsync(token, {
528-
secret: jwtConstants.secret,
529-
});
527+
// 💡 Here the JWT secret key that's used for verifying the payload
528+
// is the key that was passsed in the JwtModule
529+
const payload = await this.jwtService.verifyAsync(token);
530530
// 💡 We're assigning the payload to the request object here
531531
// so that we can access it in our route handlers
532532
request['user'] = payload;

0 commit comments

Comments
 (0)