elysiajs / elysia-jwt

Plugin for Elysia for using JWT Authentication
MIT License
34 stars 14 forks source link

@elysiajs/jwt ^1.0.2 breaks type inference for handler functions #26

Open Limitless-Kode opened 5 months ago

Limitless-Kode commented 5 months ago

I added @elysiajs/jwt ^1.0.2 to my project and initialised it with the .use hook. Everything seems to work fine however it breaks the type inference causing the handler method parameters to implicitly return a type of any.

    app.use(jwt({
        secret: env.JWT_SECRET,
        algorithms: ["HS256"],
        maxAge: "1d",
        clockTolerance: 0
    }))
    .get("/", async ({query, jwt, set, cookie: { auth }}) => {
            const verified = await jwt.verify(auth.value)
            if (!verified) {
                set.status = 401
                return 'Unauthorized'
            }
            const users = await AuthenticationController.all(query);
            return ApiResponse.success(users);
        },
        {
            detail: { tags: ["Authentication"] }
        }
    )

jwt, query, set, and auth now implicitly have the type of any

luccaparadeda commented 4 months ago

Up

RickGeersing commented 2 months ago

Any updates on this? Experiencing the same

Limitless-Kode commented 2 months ago

Any updates on this? Experiencing the same

As a fix, I decided to convert the return value to an Elysia & { jwt: typeof jwt }

jwt({
  name: "jwt",
  secret: env.JWT_SECRET,
  algorithms: ["HS256"],
  ignoreExpiration: false,
  audience: "https://api.domain.com",
  issuer: "https://api.domain.com",
  maxAge: "1d",
  clockTolerance: 0,
}) as Elysia & { jwt: typeof jwt };
RickGeersing commented 2 months ago

Thanks, that is a workable fix for now

RickGeersing commented 1 month ago

Updating to the latest version (1.1.0) fixed it for me