Skip to content

Rails 5 API authentication with Warden and JSON Web Token

Notifications You must be signed in to change notification settings

lucatironi/rails-5-api-jwt

Repository files navigation

README

Code Climate Test Coverage Build Status

Rails 5 API application (--api) using Warden to authenticate requests with JSON Web Tokens. Updated from my other repository and using Docker Compose.

Setup the app

Build the docker containers:

$ docker-compose build

Bundle the gems (it uses a volume):

$ docker-compose run bundle

Run the bin/setup script

$ docker-compose run web bin/setup

Running and testing the app

Run the containers

$ docker-compose up

Run the specs

$ docker-compose exec web bundle exec spec

Test the API with Curl

$ curl -i -X POST -H "Content-Type:application/json" \
-d '{"email":"user@example.com", "password":"password"}' \
http://localhost:3000/authentications.json

The response will contain the JSON Web Token

{"auth_token":"eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIloxLCJleHAiOjE1MTA1MTcwMzd9.S4c2YwqajvZupSQdeK5dwn8h8JSO90S851ua36Gz2s0"}

Use it with another request to an authenticated endpoint, passing it as a an Authorization header

$ curl -i -H "Content-Type:application/json" \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIloxLCJleHAiOjE1MTA1MTcwMzd9.S4c2YwqajvZupSQdeK5dwn8h8JSO90S851ua36Gz2s0" \
http://localhost:3000/customers.json

About

Rails 5 API authentication with Warden and JSON Web Token

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published