elysiajs / elysia-jwt

Plugin for Elysia for using JWT Authentication
MIT License
38 stars 16 forks source link

Property 'jwt' does not exists on context. #40

Closed jakmaz closed 1 month ago

jakmaz commented 1 month ago

I am having trouble in using jwt property in routes in different files. Why is jwt not present although using functional callback?

app.ts

import jwt from "@elysiajs/jwt";
import Elysia from "elysia";
import { env } from "../env";
import { auth } from "./auth";

const app = new Elysia()
  .group("/api", (app) =>
    app
      .use(
        jwt({
          name: "jwt",
          secret: env.jwtSecret,
        }),
      )
      .use(auth),
  )
  .listen(8080);
console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);

auth.ts

import Elysia from "elysia";

export const auth = (app: Elysia) => app.get("/auth", ({ jwt }) => "auth");
// Property 'jwt' does not exists on type...
jakmaz commented 1 month ago

I got the answer in elysiajs discord server.

Message from @bogeychan you must use .use(jwt()) on all instances where you need the type:

// auth.ts

import { Elysia } from "elysia";
import { jwt } from "@elysiajs/jwt";

export const myJwt = jwt(/* ... */);

export const auth = new Elysia({ name: "auth" })
  .use(myJwt)
  .get("/auth", ({ jwt }) => "auth");