# How to Add JWT Auth API to Existing Rails Application

Photo by [Jordan Harrison](https://unsplash.com/@jordanharrison?utm_source=medium&utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&utm_medium=referral)

First things first, add ‘JWT’ and ‘Blueprint’ gems to your `Gemfile`.

JWT gem is a ruby implementation of the [RFC 7519 OAuth JSON Web Token (JWT)](https://tools.ietf.org/html/rfc7519) 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.

So, your `Gemfile` should contain these new lines:

%[https://gist.github.com/4835b4184f61393f3f48b9cf7a3b920f]

Then run `bundle` in the Terminal.

To be able to access API pages, add them to the `routes.rb` file:

%[https://gist.github.com/60542ac9a34c57ec879fdd5afa766174]

Create `app/controllers/api` folder and put the `api_controller.rb` file there:

%[https://gist.github.com/29ba282289564510bd79030e07e35ba2]

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.

%[https://gist.github.com/fad1714fd3f0a9dd598a4ee2947c237e]

Implement the authorization feature in `sessions_controller.rb`:

%[https://gist.github.com/8fe80b6c5860c791bd30813e465fbb69]

To test the API we can try to get the profile data for an authenticated user with the `users_controller`:

%[https://gist.github.com/ef7324e170c2b186e6fca95d7a2d6e6e]

Add Blueprint that will transform object’s data to JSON:

%[https://gist.github.com/6b496ac6e9c7a733bd0187e67ae9fd0b]

That’s it! You have JWT Auth API with DRY Monads in your existing Rails application.
