graphene icon indicating copy to clipboard operation
graphene copied to clipboard

401 status code for token authentication

Open berkcoker opened this issue 5 years ago • 4 comments

I think in the case of token authentication, it would make sense to be able to return a 401, even if only one field errors out this way (referring to the example given in the original post). This status code is necessary to make this library compatible with authMiddleware in React Relay Network Modern, which expects a 401 status code to refresh tokens.

Originally posted by @berkcoker in https://github.com/graphql-python/graphene/issues/946#issuecomment-698617718

berkcoker avatar Sep 24 '20 22:09 berkcoker

any progress on this open issue?

goharyounas avatar Apr 07 '21 07:04 goharyounas

Definitely, by not being able to return 401, graphene is de facto incompatible with the widely used React Relay Network Modern.

Solution 1: (Suboptimal) : I have found a hackish way to solve it here https://stackoverflow.com/questions/49349689/how-to-return-customized-json-response-for-an-error-in-graphene-django-graphen ... really suboptimal compared to the advantage of being able to set up status codes in the resolvers logic directly.

Solution 2 (Better but Undocumented) : By looking at https://github.com/relay-tools/react-relay-network-modern/blob/master/src/middlewares/auth.js we should be able to trigger the reload of tokens by returning the error message 'Empty token' , line 51

advl avatar Apr 11 '21 12:04 advl

Is solution 2 actually viable? I believe the error message that is being read is internal to the lib and not in the request response.

@advl have you found a solution to this issue?

Shannanigans avatar Sep 16 '21 16:09 Shannanigans

For anyone else ending up here. I solved this by getting the token payload in a django middleware which catches the jwt.ExpiredSignatureError exception and returns a 401. I had to create a custom react relay network modern middleware on the client to exclude auth headers.

Shannanigans avatar Sep 20 '21 16:09 Shannanigans