Rails 7 #126 API from Zero. Bearer authentication

26/04/2023 (12 months ago)
rails api bearer authentication api testing devise api

Let's build a public REST API with Rails!

Have you ever obtained API keys to a service, using which you could read and write data? For example, Twitter, Slack or Github API?

In this mini series we will build an API where users can authenticate via Bearer tokens and perform CRUD actions. We will make API requests via cURL and Faraday, generate API documentation with OpenAPI/Swagger, and write tests for our API.

In this first episode we will build the backend functionality for users to be able to create API keys and use them to make authenticated requests to your applications public API.

Episode source code: https://github.com/corsego/126-api-bearer-authentication/commit/24bdf07d38d84528ae551b1e6d065fe3343716b1
Based on this blogpost: https://blog.corsego.com/rails-api-bearer-authentication

0:00 create an api and make a cURL request
2:39 ApiToken model. generate tokens for users
7:55 Bearer authentication via ApiToken
16:15 render current_api_token and current_user in json response
17:16 writing tests for API authentication
21:40 summary

Sign in to join the conversation