Logged User
Explore how to implement user authentication in a Rails API by creating a current_user method that retrieves the user based on JWT tokens in HTTP headers. Understand setting up supporting files, writing tests, and integrating the method into the application controller to secure API endpoints requiring authorization.
We'll cover the following...
We implemented the following logic: the API returns the authentication token to the client if credentials are correct. We’ll now implement how to find the corresponding user of the authentication token given in the HTTP header. We’ll need to do so each time this client requests an entry point that requires permission.
We will use the HTTP header Authorization for this purpose. We can also use a GET parameter named apiKey but will be using an HTTP header to give context to the request without polluting the URL with additional parameters.
We will, therefore, create a current_user method to meet our needs. It will find the user associated with the authentication token sent with each request. All the associated methods will ...