# I'm getting \`UnauthorizedError: jwt malformed at new UnauthorizedError\` with \`credentialsRequired: false\`

**URL:** <https://community.auth0.com/t/im-getting-unauthorizederror-jwt-malformed-at-new-unauthorizederror-with-credentialsrequired-false/85400>\
**Category:** JWT.io\
**Created:** [June 20, 2022, 8:04am UTC](https://community.auth0.com/t/im-getting-unauthorizederror-jwt-malformed-at-new-unauthorizederror-with-credentialsrequired-false/85400 "2022-06-20T08:04:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norby](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/norby/32/33806_2.png) [@Norby](https://community.auth0.com/u/Norby)\
**Post date:** [June 20, 2022, 8:04am UTC](https://community.auth0.com/t/im-getting-unauthorizederror-jwt-malformed-at-new-unauthorizederror-with-credentialsrequired-false/85400/1 "2022-06-20T08:04:07Z")

</div>

I’m trying to implement an API endpoint where presence of a token is optional. If I understand this correctly, the way to do this is to add `credentialsRequired: false` as one of the params to the middleware.  
However, requests made to this endpoint that do not have a token, result in `UnauthorizedError: jwt malformed at new UnauthorizedError`

So just to make sure it’s clear, it does not appear to be a problem with validating the tokens in itself, because if the token is present everything works as expected. But the error shows up without a token.

My checkJwt is configured as follows

```auto
const { expressjwt } = require("express-jwt");
const jwksRsa = require("jwks-rsa");
const domain = process.env.AUTH0_DOMAIN, audience = process.env.AUTH0_AUDIENCE;

const checkJwt = expressjwt({
	secret: jwksRsa.expressJwtSecret({
		cache: true,
		rateLimit: true,
		jwksRequestsPerMinute: 5,
		jwksUri: `https://${domain}/.well-known/jwks.json`
	}),
	credentialsRequired: false,
	audience: audience,
	issuer: `https://${domain}/`,
	algorithms: ["RS256"]
});

module.exports = {
	checkJwt
};

```

The route in express is as follows

```auto
router.post('/membership/upgrade', checkJwt, require('./handlers/membership').upgrade);

```

I have checked the request headers on the client side and it’s not adding `Bearer`.

Can anybody point out what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![Norby](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/norby/32/33806_2.png) [@Norby](https://community.auth0.com/u/Norby)\
**Post date:** [June 21, 2022, 7:24am UTC](https://community.auth0.com/t/im-getting-unauthorizederror-jwt-malformed-at-new-unauthorizederror-with-credentialsrequired-false/85400/2 "2022-06-21T07:24:29Z")

</div>

Worked out what the issue is. Turns out it _was_ sending the header, but I missed it because it was sending `null` instead of omitting the header completely

---

<div class="post-metadata">

**Author:** ![bessiegarz68](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/bessiegarz68/32/33412_2.png) [@bessiegarz68](https://community.auth0.com/u/bessiegarz68)\
**Post date:** [June 25, 2022, 7:22am UTC](https://community.auth0.com/t/im-getting-unauthorizederror-jwt-malformed-at-new-unauthorizederror-with-credentialsrequired-false/85400/3 "2022-06-25T07:22:41Z")

</div>

This issue is pretty straightforward: In most circumstances, it’s logically disjointed to allow the forward sequence of a request without credentials, yet deny a request that has expired credentials[.](https://www.myccpay.net/)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 14, 2024, 10:45pm UTC](https://community.auth0.com/t/im-getting-unauthorizederror-jwt-malformed-at-new-unauthorizederror-with-credentialsrequired-false/85400/4 "2024-11-14T22:45:22Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
