How to Add JWT Auth API to Existing Rails Application
First things first, add ‘JWT’ and ‘Blueprint’ gems to your
JWT gem is a ruby implementation of the RFC 7519 OAuth JSON Web Token (JWT) standard.
Blueprinter is a JSON Object Presenter for Ruby that takes business objects and breaks them down into simple hashes and serializes them to JSON.
Gemfile should contain these new lines:
bundle in the Terminal.
To be able to access API pages, add them to the
app/controllers/api folder and put the
api_controller.rb file there:
To implement the registration feature, create this
registrations_controller.rb file. Note, that standard registration logic is reused from the version of the application before the API feature implementation. Here we used the DRY Monads.
Implement the authorization feature in
To test the API we can try to get the profile data for an authenticated user with the
Add Blueprint that will transform object’s data to JSON:
That’s it! You have JWT Auth API with DRY Monads in your existing Rails application.