My First Hackathon: Using Faraday and Firebase for Authentication

Andrew Smoker
3 min readAug 5, 2021

I knew I would learn a lot by participating in my first Hackathon and that has proven to be true just 2 days into the project. When setting up user authentication for this project, my teammates recommended using Firebase and Faraday which I had not heard of, but wanted to learn. So today I will give a brief introduction to using these in a Rails application.

The first step is to make sure to require the Faraday gem in your Gemfile.

gem "faraday", "~> 1.6"

Faraday is an http client that helps process the request/response cycle. Once the gem is installed I can make requests by calling different methods on Faraday. For example, if I wanted to fetch all the moves from the Pokemon API, I could simply call the following and set it to a variable (response). This returns a Faraday::Response Object that comes with a few built-in methods we can use. I can call response.status and get the status code, response.headers to get an object of key/value pairs from the site, or response.body to get the returned content.

response = Faraday.get("https://pokeapi.co/api/v2/move")response.status
//200
response.headers
//# => {"server"=>"Fly/c375678 (2021-04-23)", "content-type"=> ...
JSON.parse(response.body)
//{
"count": 844,
"next": "https://pokeapi.co/api/v2/move?offset=20&limit=20",
"previous": null,
"results": [
{
"name": "pound",
"url": "https://pokeapi.co/api/v2/move/1/"
},

I can also make post requests by passing in parameters. The parameters are the url, a body object and a header object as needed.

url = "https://pokeapi.co/api/v2/move/1/"
body = {
email: [email]
}
response = Faraday.post(url, JSON.generate(body), 'Content-Type' => 'application/json')

There are a lot of settings options that can be configured using Faraday and I would encourage you to look into them more. But the basics of processing the request/response cycle is made much simpler by using Faraday!

Now I want to talk about Firebase. Firebase is a backend platform for web applications that offers real time database, authentications and more. For now, I am going to talk about using it as a way to create and authenticate users for your application.

The first thing to do is to create and register your project with Firebase. Next, go to Build, then Authentication which brings up a lot of different options of how to verify users. I want to enable the email/password option for now. I also need to add an App to my project in order to get a Web API key. This can be found in the general tab under Project Settings. Now I am ready to start creating/authenticating!

Back to the Rails app. First I add the Web API key to a dotenv file (or an alternative way of accessing environment variables). To create a user, I will use the designated url from Firebase along with the api key from our environment variable. The body needs to include an email and password as well as ‘returnSecureToken: true’. The header must be ‘Content-Type’ => ‘application/json’. Using Faraday, I make a post request (to create the user) and assign it to a variable. Finally parse the response body to get the data. All I really need to save in my db is the UID returned from Firebase and they handle the rest! Now that the user has been created, when they login again, I can use Faraday with a different url to verify the user.

url = "https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=#{ENV['API_KEY']}"body = {
email: params[:email],
password: params[:password],
returnSecureToken: true
}
resp = Faraday.post(url, JSON.generate(body), 'Content-Type' => 'application/json')data = JSON.parse(resp.body)render json: data

This is just a very small part of what Firebase has to offer, but definitely check it out if you are looking for a Baas. It’s always fun to learn different ways of approaching problems and using Firebase and Faraday was a great start to expand what I currently know!

--

--

Andrew Smoker

I am 34 years old and making a huge career change by attending Flatiron School’s Software Engineering Bootcamp. Excited to learn!