#Debugging 401 Error

1 messages · Page 1 of 1 (latest)

red radish
#

Trying to understand why we're getting a 401 unauthorized error. Just started yesterday, but was working before.

Any ideas on where to look?
We have a cookie, it's in use, and our JWT Strategy was updated to use the cookie instead of an auth bearer token.

Seems you fix one thing, and break another.

zealous beacon
#

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);
  }
}
red radish
#

Looks like I get to make a tuple.
"A spread argument must either have a tuple type or be passed to a rest parameter."

zealous beacon
#

Hmm, Could've sworn I had this worked out type wise

#

Typescript Playground generally seems fine with it

red radish
#

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);
    }
}
zealous beacon
#

Hmm, looks like it doesn't understand what type the args actually is here

#

AuthGuard is from @nestjs/passport, right?

red radish
zealous beacon
#

Typescript version?

red radish
#

"typescript": "^4.3.5"

zealous beacon
#

Ah, now I get it in the playground. Okay, just a minute. Let me play around with the typings

red radish
#

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"
    }
}
zealous beacon
#

Found it! Here's the correct type: Parameters<InstanceType<ReturnType<typeof AuthGuard>>['handleRequest']>

#

Needed InstanceType instead of going through the prototype

red radish
#

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
]
zealous beacon
#

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

red radish
#

Well, at least I've got an error that tells me something now.