Search code examples
node.jsreact-nativeauthenticationreact-navigationtoken

error 401 unauthorized I see when I am successfully logged in. But after logging in, the token gets Revoked and the response is error 401 unauthorized


that error appears to me [Unhandled promise rejection: Error: Request failed with status code 401]

that's the Ropo link https://github.com/ahmedsawy1/Carna-Project

It contains two folders, the first for the front end (React Native) and the other for the back end (Node js)

Everything works fine regarding Authentication where I can register a user or login with a valid password and email and bring the token and everything is fine. But the problem is that when I log in.. and bring a correct token, it works and actually logs in. This app supposedly goes to the Profile screen after logging in. But after The successful login, it goes to the Profile screen, and here the problem appears, which is that the token gets Revoked and does not show any data for the user, and I cannot bring his data such as his name or his data to display on his personal page. I would appreciate someone helping me here.


Solution

  • I think you use JWT for your authentication. JWT contains accessToken and refreshToken. both of this tokens have expired time and refreshToken has longer expire time. whenever accessToken expired, it would be usually received error code 401. By getting this error, you need to send your accessToken and your refreshToken to the refreshToken api and get the new accessToken and RefreshToken and carry on with your new accessToken