DEV Community

loading...

Discussion on: API Guard - JWT authentication solution for Rails APIs

Collapse
borasumer profile image
Bora Sumer

Thank you for quick reply Gokul. I am a little bit confused with this authentication stuff to be honest. What I am trying to achieve is that I want to keep users logged in on the application even after they refresh so they would not have to submit their credentials every time they open the application. I am working on a React-Native mobile app. So basically I assume I need to store the JWT in the localStorage and use it every time I send a request to the server to reach the protected controllers/data. But what about user info(username, email, name ,last_name), I want to display those info on the page after the app is refreshed too, do I store them in the localStorage too when I sign in once?
Sorry for beginner questions.
Thanks a lot man.

Thread Thread
gokul595 profile image
Gokul Murali Author

Hi Bora, You are right. You can store the JWT in the local storage and pass it in the request header every time in the API.

For displaying current user profile you can create an API (Ex: /profile) and just add the before_action: authenticate_and_set_user and you will have the current logged in user object in the @current_user instance variable which you can send in the response and use it in the Front end. You can call this API request whenever your web app loads in the browser and show those details.

Let me know if this clarifies.

Thread Thread
borasumer profile image
Bora Sumer

Yeah, I have handled it that way, the only thing it is missing is the forgot password functionality. I think I can just update the password field of the user by sending a a request to the controller with the token. That should do it securely I assume. Thank you for your help again Gokul. Really appreciated.