I'm trying to get the information stored in my jwt in Nestjs.But I'm getting the following error
[Nest] 16960 - 19/12/2022, 10:22:28 pm ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'decode') TypeError: Cannot read properties of undefined (reading 'decode')
at AdminGuard.canActivate (D:\Noum\Data\CYBRNODE\MAN STACK\Cybrnode-Blog-Backend\Cybr-Blog-Nest-Backend\src\guard\admin.guard.ts:28:34)at GuardsConsumer.tryActivate (D:\Noum\Data\CYBRNODE\MAN STACK\Cybrnode-Blog-Backend\Cybr-Blog-Nest-Backend\node_modules@nestjs\core\guards\guards-consumer.js:15:34) at processTicksAndRejections (node:internal/process/task_queues:96:5)
code
export type PayloadType = {
id: string;
};
code. I can see that token is present through console
let jwtService: JwtService;
let token = AuthenticationService.jwtToken.accessToken;
console.log(token);
if (token) {
const payload = jwtService.decode(token.split('')[1]) as PayloadType;
console.log(payload);
}
Always make sure the Imports
I was using
import { JwtService } from '@nestjs/jwt';
But according to @mh377 the following import is used to decode
import * as jwt from 'jsonwebtoken';
Example according to my code
let token = AuthenticationService.jwtToken;
var { header, payload, signature } = jwt.decode(token.accessToken, {
complete: true,
});
console.log('ID', payload.sub);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.