elysiajs / elysia-jwt

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

Jwt verify problems #15

Open ImJustLucas opened 1 year ago

ImJustLucas commented 1 year ago

Hello guys, i got a problem with the .verify method of the @elysia/jwt package. I'm trying to verify my jwt token but it doesn't work and return false. jwt.io tell me there is nothing wrong with my token

here is my code:

  const jwt = await jwtAccess.verify(cookie!.access_token);
  console.log("Test verify JWT", jwt, cookie!.access_token);
  if (!jwt) {
    set.status = 401;
    return {
      success: false,
      message: "Unauthorized",
      errors: "Invalid access token",
    };
  }

this code is in a middleware, and the cookie is set in the request. the code of the jwtAccess.verify method is:

export const jwtAccessSetup = new Elysia({
  name: "jwtAccess",
}).use(
  jwt({
    name: "jwtAccess",
    schema: t.Object({
      userId: t.String(),
    }),
    secret: process.env.JWT_ACCESS_SECRET || "DO NOT USE THIS SECRET KEY",
    exp: 30 * 24 * 60 * 60,
  })
);

Even this, return false

const test = await jwtAccess.sign({ userId: "652e83d3b47c67a8d823daa6" });
console.log("@Cookie verify", await jwtAccess.verify(test));

every time i want to check the jwt, it returns me false but when i check it with the jwt.io debugger, it works. is there a problem with the verify method or am i doing something wrong?

full repo: https://github.com/ImJustLucas/bun-elysia-boilerplate/tree/master

MariuzM commented 11 months ago

Might be related to what i want https://github.com/elysiajs/elysia-jwt/issues/17

ImJustLucas commented 11 months ago

We both got issue on .verify, my token here is not expired i'm sure about that

silverlyra commented 4 months ago

Verification can fail if there’s a schema defined. I’ve published my fork of this library which fixes that issue.

ImJustLucas commented 4 months ago

Hello, i've forgot to write it here, but i've fixed this issue on my repo where i used it, with still the schema defined

here is what i changed, my problem was due to the exp property which doesn't take the actual date by default

image

after, thanks to new version of elysia, i could remove the @elisya/cookie package, and use the native usage if cookie

image