If I am not using cookies and relying on JWT. The verifySession method on express framework is not g...
h
If I am not using cookies and relying on JWT. The verifySession method on express framework is not good. I googled and got this custom method. https://supertokens.com/docs/thirdparty/common-customizations/sessions/session-verification-in-api/get-session#getsession-vs-verifysession But unfortunately, the session is always empty. I have a proper header with Authorization: Bearer JWT_TOKEN format. Any heads up for what possibly be the case? Oh also since I am usign Hasura, I have enabled custom claims and I have enabled JWT with session.
r
Hey @hitesh.io you will need to verify the JWT using any JWT verification lib. Our verifySession function doesn’t work with a JWT, and only works with the supertokens access token.
h
Any examples you can point me to?
h
So this requires JWKS_URI Can I use the same method as supertokens that generates the URI? The same server is generating and consuming it, it doesnt make sense to route the consumtion through HTTP
r
You could hard code the public key in your backend app. There is a section about that too in our docs. The same page as above.
h
If I hard code it, does it change over time?
r
It doesn’t.
h
Do I need to pass the same certificate to supertokens core or the custom supertoken auth server?
r
no
h
I wrote this as a verifySession middleware. export function verifySession() { return async (req: Request, res: Response, next: NextFunction) => { if (!req.headers.authorization) { return res.status(401).send("Authorization header is required"); } let jwt = req.headers.authorization.split(" ")[1]; console.log(process.env.JWKS_CERT!); console.log(jwt); JsonWebToken.verify(jwt, certificate, {}, function (err, decoded) { let decodedJWT = decoded; // Use JWT console.log(decodedJWT); console.log(err); }); next(); }; } but it says
JsonWebTokenError: secret or public key must be provided
Any suggestions?
yea.. the docs are clear enough 🙂
can't really help further on this point
h
Copy code
export function verifySession() {
  return async (req: Request, res: Response, next: NextFunction) => {
    if (!req.headers.authorization) {
      return res.status(401).send("Authorization header is required");
    }
    let jwt = req.headers.authorization.split(" ")[1];
    console.log(process.env.JWKS_CERT!);
    console.log(jwt);
    JsonWebToken.verify(jwt, certificate, function (err, decoded) {
      let decodedJWT = decoded;
      // Use JWT
      console.log(decodedJWT);
      console.log(err);
    });
    next();
  };
}
Modified as per your function.
ya I know, its jsut the same code isnt decoding jwt. certificate and jwt both are present.
sorry if I am asking too much. Will debug
77 Views