This is an example of a Rails 8 API-only app that uses Devise JWT for authentication.
git clone git@github.com:coderhs/rails-api-only-devise-jwt-example-app.git
Create the credentials.yml.enc
file:
EDITOR="vim" rails credentials:edit
Copy the contents from credentials.example.yml
into your new file:
secret_key_base:
devise:
jwt_secret_key:
Fill in both fields with secret tokens, which you can generate using the command rails secret
Save and close vim
curl -i -X POST http://localhost:3000/signup \
-H "Content-Type: application/json" \
-d '{"user": {"email": "test@example.com", "password": "password"}}'
You will receive the JWT token in the response.
curl -i -X POST http://localhost:3000/login \
-H "Content-Type: application/json" \
-d '{"user": {"email": "test@example.com", "password": "password"}}'
The JWT token will be returned in the response headers.
curl 'http://localhost:3000/secret'
Response
{"error":"You need to sign in or sign up before continuing."}
curl 'http://localhost:3000/secret' -H 'Authorization: Bearer <token>'
Response
😉 Try and find out