You need to add before_action in the controllers where you need to allow only authenticated users to access. For example, if you have PostsController and if you need to allow only logged in user to access the resources you need to add the authentication before action in that controller. If the request contains valid access token in the header you can access the current user using the current_user method or @current_user variable.
Authentication controller is responsible for sign in (with email & password by default) and sign out where it will respond with the Access token after successful sign in and you need to use it in the requests wherever you have authentication enabled.
And, you get "Invalid login credentials" when you try the Sign in API with invalid email/password.
Basically here is the flow,
Get the access token from the sign in API and use it in the API requests wherever you added authentication. And, to add authentication for any of your controller just add the before_action: authenticate_and_set_user in that controller.
Hope this will clarify your doubts. Feel free to reach out to me if you need further clarifications.
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.
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.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Bora,
You need to add
before_action
in the controllers where you need to allow only authenticated users to access. For example, if you havePostsController
and if you need to allow only logged in user to access the resources you need to add the authentication before action in that controller. If the request contains valid access token in the header you can access the current user using thecurrent_user
method or@current_user
variable.Authentication controller is responsible for sign in (with email & password by default) and sign out where it will respond with the Access token after successful sign in and you need to use it in the requests wherever you have authentication enabled.
And, you get "Invalid login credentials" when you try the Sign in API with invalid email/password.
Basically here is the flow,
Hope this will clarify your doubts. Feel free to reach out to me if you need further clarifications.
Happy to help :-)
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.
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 thebefore_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.
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.