How to use OmniAuth with Rails Applications

Today I’m going to walk through using omniauth in your applications to implement logging in with Google.

Step 1: In your Gemfile add the following line of code

Adding the OmniAuth Gem to you Gemfile

Note; You’ll also need to add the gem for google oauth2, shown below.

Google OmniAuth Gem

Step 2: Once you have added the necessary gems run

Bundle Install to install newly added gems

Step 3: Next you’ll need to visit https://code.google.com/apis/console/ and go to the credentials tab, once there you will “Create Credentials” and take note of the Client ID and Client Secret.

Step 4: Create a new file within config/initializers called omniauth.rb and within that file you will need the following code .

Step 5: Now in your config/secrets.yml file you will add the following. You’ll need to add your Google Client ID and Google Secret that you obtained earlier from the console.

Step 6: Now you can set up your controller, something like this will work just fine.

And:

Step 7: In your view;

BOOM! You are done! You can now successfully login with Google on your Ruby on Rails app. If you ran into any trouble here feel free to reach out to me or take a look at the documentation here; https://github.com/zquestz/omniauth-google-oauth2

Thanks for reading and I hope this is helpful!