In this project, an API for user authentication was implemented using the JSON Web Token (JWT) devise.
The following technologies were used for this:
- ruby 2.6.5
- rails 6.1.7
- pg 1.1
How to set up:
- Clone this project
- In the project folder run the command
bundle install - Generate a token with
rake secretand copy the value displayed on .env file. The project has a .env.example as a template - Run the commands to create the databases
a.
rails db:createb.rails db:migrate
After configuration, it is possible to run the project (rails s) and make tests to access the endpoints.
Some sample tests that might be helpful
-
Try to access an authenticated controller without a JWT token
$ curl -XGET -H "Content-Type: application/json" http://localhost:3000/member-data
-
Register an account
$ curl -XPOST -H "Content-Type: application/json" -d '{ "user": { "email": "test@email.com", "password": "12345678" } }' http://localhost:3000/users
-
Login with registered account
$ curl -XPOST -H "Content-Type: application/json" -d '{ "user": { "email": "test@email.com", "password": "12345678" } }' http://localhost:3000/users/sign_in
-
It is possible to add the
-iflag in the request above to get the JWT token and evaluate that it is possible to login with just the tokena. Getting the token in the header
$ curl -XPOST -H "Content-Type: application/json" -i -d '{ "user": { "email": "test@email.com", "password": "12345678" } }' http://localhost:3000/users/sign_in
b. Logging in with the obtained token
$ curl -XGET -H "Authorization: Bearer <TOKEN JWT HERE>" -H "Content-Type: application/json" http://localhost:3000/member-data
-
Finally, we can logout, which includes the JWT Token in the deny list and does not allow it to be used again for login (ps: a new token is generated with each new login)
$ curl -XDELETE -H "Authorization: Bearer <TOKEN JWT HERE>" -H "Content-Type: application/json" http://localhost:3000/users/sign_out