Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - Logout user when api status code is 401 ( Unauthorized )

Tags:

react-native

When user login or register it will generate a token by our web server and store in user mobile. Every time an API is call, the token is required to verify by our web server to see whether the user is authorized.

Now if the token is expired, it will return status code 401. I wonder how i should handle it?

Our apps is using redux and redux action, it can be very easily doable in the reducers, but the reducers are divided up into different modules, which means i have to do it all over the place.

If you all need any info i will update my post.

like image 482
Darren Lau Avatar asked Oct 15 '17 09:10

Darren Lau


Video Answer


1 Answers

This depends on what you are using to make API calls in your app. If you are using GraphQL/Apollo, for example, you can use apollo-link-error to check if the error's status code is 401 and log the user out if it is. If you are using Axios, you can either specify the same kind of thing when you create the instance, or use something like axios interceptors to do it.

If you include what you are using to make requests in your app, I can give a more specific answer as far as the actual configuration goes.

like image 118
spjack Avatar answered Nov 15 '22 06:11

spjack