for some reason ^ crashes server, any help would b...
# prisma-whats-new
w
for some reason ^ crashes server, any help would be appreciated 🙂
l
Sounds like your transpiler isn't set up correctly.
w
it’s default setup
prisma init app
l
Ok
w
let’s say that my token is malformed/invalid in playground i get RED
TypeError: Failed to fetch
in terminal i see the error thrown nicely but it was not picked up by the server. now im looking at the source code trying to figure it out. it’s express under the hood 🙂
l
Did you set up the .env?
w
yes. it all works when token is OK. meaning not expired not malformed
l
Right, that's expected behavior.
The server should have thrown a 401 (Unauthorized)
w
it’s throwing 500 instead of 401
with error
Error: invalid signature
. I’m intentionally breaking the validity of a token. so that is not something that is unexpected. what’s unexpected is that it doesn’t handle the errors correctly
yup, same happens when jwt is expired 😞
l
Sending a malformed token is a network error. Odd that you're getting 500 instead of 401 though
w
401 should be triggered by jwt expired, right?
l
Yes
w
hm.
template for that is same/similar as mine?
l
It's based off my authentication template which is based of off an unauthenticated prisma template. 🙂
w
oh yeah, i’ve seen this one somewhere 🙂
l
Oh I just noticed/remembered that I returned the 401 from a malformed JWT. My mistake
The JWT validation only throws an error. Sorry for the bad info.
w
just checking server/src/index and it really doen’t look that diff than mine
no probs dude! happen to the best of us 😉
l
Copy code
<http://server.express.post|server.express.post>(
  server.options.endpoint,
  checkJwt,
  (err, req, res, next) => {
    if (err) return res.status(401).send(err.message)
    next()
  }
)
w
95% similar as mine
and
checkJwt
99% same 😄
l
Ok so back to your original question.. you want a malformed/expired JWT to continue?
w
no. i don’t want it to break, i would like to return anything, something except 500. 401 would be good as well. im looking at your example and comparing. what’s NOT in mine is the
prisma-binding
would that make any difference if i would inclode it as well?
l
It shouldn't. Have you console logged your error to see what's coming back?
w
errors stack-trace. i think i’ll call it for today. Thanks a lot for all the help! i’ll Post back here when i make it work 🙂
l
err
should be returning something like..
Copy code
...
 name: 'UnauthorizedError',
  message: 'jwt expired',
  code: 'invalid_token',
  status: 401,
...
w
oh that would be so nice. does your example returns such response?
l
w
yes, exactly
and then playground fails and graphiql
l
That's just a result of the network error
It'll do the same thing if you return anything other than a GraphQL structure
I think of it like this.. a client should not be sending the server an expired JWT. That should be handled on the client before the request. And no one should ever send a malformed JWT. "Bearer" means it's valid on its face. Well, it's not valid on its face, therefore, in my mind, this is a network/request level error and the server should discard the request.
w
both points true and i agree, but you know that when you are building server and expecting 3rd party apps to connect both of the points are not going to be 100% respected thus i’d really like to make it gracefully fail. there is a way but it’s on resolver level. then throwing an Error would work
l
Not to get into philosophical debates -- the server should work how you want it to work -- a network error is a graceful fail and those clients should be able to handle that. But if you actually want to return a GraphQL error, I would just attach it onto the request and deal with it later
Copy code
<http://server.express.post|server.express.post>(
  server.options.endpoint,
  checkJwt,
  (err, req, res, next) => {
    if (err) req.user = { ... authorized: false}
    next()
  }
)
w
something like that. thanks !! I hope i didn’t take much energy from you 🙂 ’twas a pleasure
l
Not at all, let me know what other issues pop up. Hopefully that helped
👍 1
w
it did.