koajs / jwt

Koa middleware for validating JSON Web Tokens
MIT License
1.34k stars 120 forks source link

TypeScript typings (and documentation) wrong for getToken function #126

Closed selfinterest closed 6 years ago

selfinterest commented 6 years ago

The typings for Options indicate that getToken's first parameter should be the options for the middleware. The documentation says that getToken's first parameter should be the options for the middleware and that "this" inside the function should be the Koa context.

In the code itself, the first parameter to getOptions is (very sensibly) the Koa context, while the options are the second parameter.

To get around this, I used a hack like this:

// "get" below is lodash's get method.
const koaJwtOptions = {
    secret: config.secret,
    getToken(opts: any) {   // opts is ACTUALLY Koa.Context
        const jwt = get(opts, "query._j") || get(opts, "query.jwt")
        return jwt;
    }
  }
sdd commented 6 years ago

Hi @selfinterest - this should be fixed now, as of v3.4.0. See https://github.com/koajs/jwt/blob/master/types/index.d.ts#L17

Thanks!