Skip to content
Matthew Sullivan edited this page Jun 13, 2024 · 6 revisions
Stack Logos

Ruby, JWT, and Postgres Authentication

Lightweight Ruby on Rails user registration/login GraphQL API that returns stateless json web tokens to access secured mutations and queries.

Database

Install Postgres

https://postgresapp.com/

Create database

$ createdb rjpa_test

Setup

Install dependencies and build database

$ bundle install && rails db:migrate

Serve

$ rails s

cURLS

Register

curl --location --request POST 'http://127.0.0.1:3000/graph' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation($input: RegisterInput!) {register(input: $input) {email firstName lastName}}","variables":{"input":{"firstName":"[FIRST_NAME]","lastName":"[LAST_NAME]","authProvider":{"credentials":{"email":"[EMAIL]","password":"[PASSWORD]"}}}}}'

Login

curl --location --request POST 'http://127.0.0.1:3000/graph' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation ($input: LoginInput!) {login (input: $input){token user {email firstName lastName}}}","variables":{"input":{"credentials":{"email":"[EMAIL]","password":"[PASSWORD]"}}}}'

Update User

curl --location --request POST 'http://127.0.0.1:3000/graph' \
--header 'Authorization: Bearer: [TOKEN]' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"mutation ($input: UpdateUserInput!) {updateUser (input: $input){user {email firstName lastName}}}","variables":{"input":{"arguments":{"email":"[EMAIL]","firstName":"[FIRST_NAME]","lastName":"[LAST_NAME]","password":"[PASSWORD]"}}}}'

Fetch User

curl --location --request POST 'http://127.0.0.1:3000/graph' \
--header 'Authorization: [TOKEN]' \
--header 'Content-Type: application/json' \
--data-raw '{"query":"query ($input: FetchUserInput!) {fetchUser(input: $input) {user {firstName lastName id}}}","variables":{"input":{"arguments":{"id":"[USER_ID]"}}}}'

Test

$ rails test

Contributing

Pull requests are welcome. For major changes, open an issue first to discuss what you would like to change.

Please make sure to update and or write tests as appropriate.

License

MIT

Clone this wiki locally