Skip to main content

Command Palette

Search for a command to run...

How to Add JWT Auth API to Existing Rails Application

Updated
1 min read
How to Add JWT Auth API to Existing Rails Application

Photo by Jordan Harrison on Unsplash

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) 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:

Then run bundle in the Terminal.

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

Create 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 sessions_controller.rb:

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

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.

More from this blog

Невидимий дизайн: Чому «хмара часток» в Apple Watch — це геніальний інженерний хід

Кожен розробник, який хоч раз налаштовував Apple Watch, відчував цей «wow-ефект». Замість стандартного QR-коду — таємнича анімація, яку iPhone зчитує за частку секунди. Як інженер, я не міг пройти повз і не розібратися: як це працює насправді? Довгий...

Jan 27, 20262 min read
Невидимий дизайн: Чому «хмара часток» в Apple Watch — це геніальний інженерний хід

Як додати графік відключення світла у Календар?

Оновлення Ця стаття втратила актуальність після закінчення відключення світла. Останнє відключення світла у Львові було 30 грудня 2024 р. Ви можете легко додати графіки погодинних відключень світла у місті Львові в Google Календар на Android чи Apple...

Jun 22, 20241 min read
Як додати графік відключення світла у Календар?
V

Viacheslav Kharchenko

20 posts

Ukrainian Tech Lead & Rails dev.

Solving software engineering bottlenecks and daily routine friction — from smart homes to life-ops. Streamlining code and life, one fix at a time.