mirror of
https://github.com/nestjs/docs.nestjs.com.git
synced 2026-02-21 20:31:32 +00:00
docs(auth): remove redundant jwt secret in authentication code samples
The secret is already configured in JwtModule and does not need to be passed again to `verifyAsync` method of `JwtService`. Reference to issue raised: #3370
This commit is contained in:
@@ -242,6 +242,8 @@ export class AuthService {
|
||||
}
|
||||
const payload = { sub: user.userId, username: user.username };
|
||||
return {
|
||||
// 💡 Here the JWT secret key that's used for signing the payload
|
||||
// is the key that was passsed in the JwtModule
|
||||
access_token: await this.jwtService.signAsync(payload),
|
||||
};
|
||||
}
|
||||
@@ -266,6 +268,8 @@ export class AuthService {
|
||||
}
|
||||
const payload = { username: user.username, sub: user.userId };
|
||||
return {
|
||||
// 💡 Here the JWT secret key that's used for signing the payload
|
||||
// is the key that was passsed in the JwtModule
|
||||
access_token: await this.jwtService.signAsync(payload),
|
||||
};
|
||||
}
|
||||
@@ -368,7 +372,6 @@ import {
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { jwtConstants } from './constants';
|
||||
import { Request } from 'express';
|
||||
|
||||
@Injectable()
|
||||
@@ -382,12 +385,9 @@ export class AuthGuard implements CanActivate {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
try {
|
||||
const payload = await this.jwtService.verifyAsync(
|
||||
token,
|
||||
{
|
||||
secret: jwtConstants.secret
|
||||
}
|
||||
);
|
||||
// 💡 Here the JWT secret key that's used for verifying the payload
|
||||
// is the key that was passsed in the JwtModule
|
||||
const payload = await this.jwtService.verifyAsync(token);
|
||||
// 💡 We're assigning the payload to the request object here
|
||||
// so that we can access it in our route handlers
|
||||
request['user'] = payload;
|
||||
@@ -524,9 +524,9 @@ export class AuthGuard implements CanActivate {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
try {
|
||||
const payload = await this.jwtService.verifyAsync(token, {
|
||||
secret: jwtConstants.secret,
|
||||
});
|
||||
// 💡 Here the JWT secret key that's used for verifying the payload
|
||||
// is the key that was passsed in the JwtModule
|
||||
const payload = await this.jwtService.verifyAsync(token);
|
||||
// 💡 We're assigning the payload to the request object here
|
||||
// so that we can access it in our route handlers
|
||||
request['user'] = payload;
|
||||
|
||||
Reference in New Issue
Block a user