#Debugging 401 Error
1 messages · Page 1 of 1 (latest)
Add this to your JwtGuard
handleRequest(...args: Parameters<ReturnType<typeof AuthGuard>['prototype']['handleRequest']>) {
console.log(args);
return super.handleRequest(...args);
}
If you don't have a JwtGuard, make one like so:
@Injectable()
export class JwtGuard extends AuthGuard('jwt') {
handleRequest(...args: Parameters<ReturnType<typeof AuthGuard>['prototype']['handleRequest']>) {
console.log(args);
return super.handleRequest(...args);
}
}
Updated. Was missing a paren.
handleRequest(
...args: Parameters<
ReturnType<typeof AuthGuard>['prototype']['handleRequest']
>
) {
console.log(args);
return super.handleRequest(...args);
}
Looks like I get to make a tuple.
"A spread argument must either have a tuple type or be passed to a rest parameter."
Hmm, Could've sworn I had this worked out type wise
Typescript Playground generally seems fine with it
Well, this is what I've got in my JWT Guard.
// NPM Modules
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
// create jwt guard class
@Injectable()
export class JWTGuard extends AuthGuard('jwt-validation') {
constructor() {
super();
}
handleRequest(
...args: Parameters<
ReturnType<typeof AuthGuard>['prototype']['handleRequest']
>
) {
console.log(args);
return super.handleRequest(...args);
}
}
Hmm, looks like it doesn't understand what type the args actually is here
AuthGuard is from @nestjs/passport, right?
Yes. Code updated above.
Typescript version?
"typescript": "^4.3.5"
Ah, now I get it in the playground. Okay, just a minute. Let me play around with the typings
Real quick, should I be on that version?
Package.json
{
"dependencies": {
"@nestjs-modules/mailer": "^1.8.1",
"@nestjs/common": "^8.0.0",
"@nestjs/config": "^2.1.0",
"@nestjs/core": "^8.0.0",
"@nestjs/jwt": "^8.0.1",
"@nestjs/mapped-types": "^1.0.1",
"@nestjs/passport": "^8.2.1",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/typeorm": "^8.1.2"
},
"devDependencies": {
"@nestjs/cli": "^8.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"typescript": "^4.3.5"
}
}
Found it! Here's the correct type: Parameters<InstanceType<ReturnType<typeof AuthGuard>>['handleRequest']>
Needed InstanceType instead of going through the prototype
Works like a charm. You're awesome man. Time to test the endpoint
Endpoint failed. I've got to step away for a 30 min call, but I'll be back after. The error is below.
[
null,
false,
Error: No auth token
at JWTStrategy.JwtStrategy.authenticate (/veterandb.com/backend/node_modules/passport-jwt/lib/strategy.js:96:26)
at attempt (/veterandb.com/backend/node_modules/passport/lib/middleware/authenticate.js:369:16)
at authenticate (/veterandb.com/backend/node_modules/passport/lib/middleware/authenticate.js:370:7)
at /veterandb.com/backend/node_modules/@nestjs/passport/dist/auth.guard.js:96:3
at new Promise (<anonymous>)
at /veterandb.com/backend/node_modules/@nestjs/passport/dist/auth.guard.js:88:83
at JWTGuard.<anonymous> (/veterandb.com/backend/node_modules/@nestjs/passport/dist/auth.guard.js:49:36)
at Generator.next (<anonymous>)
at fulfilled (/veterandb.com/backend/node_modules/@nestjs/passport/dist/auth.guard.js:17:58)
at processTicksAndRejections (node:internal/process/task_queues:96:5),
ExecutionContextHost {
args: [ [IncomingMessage], [ServerResponse], [Function: next] ],
constructorRef: [class BusinessController],
handler: [Function: businessCreateOne],
contextType: 'http',
getRequest: [Function: getRequest],
getResponse: [Function: getResponse],
getNext: [Function: getNext]
},
undefined
]
Well, plain as day, No auth token. So however you're sending the auth token (cookies I believe you said) it's not being sent
Well, at least I've got an error that tells me something now.