DEV Community

Cover image for [AI] Setting up omni-auth in rails
anes
anes

Posted on • Updated on

[AI] Setting up omni-auth in rails

Read this article to understand why the title has the [AI] at the start.

Setting up OmniAuth with Devise in Rails

OmniAuth is a popular gem for enabling social login in Rails applications. It allows users to log in to your app using their existing accounts on third-party providers such as Google, Facebook, and Twitter. In this tutorial, we will show you how to set up OmniAuth in a Rails app using the Devise gem for authentication.

Step 1: Install the necessary gems

First, make sure you have the devise and omniauth gems installed in your Rails application. You can add these lines to your Gemfile:



gem 'devise'
gem 'omniauth'


Enter fullscreen mode Exit fullscreen mode

Then run bundle install to install the gems.

Step 2: Run the Devise and Omniauth generators

Next, we need to run the Devise and Omniauth generators to create the necessary configuration files and models. You can do this with the following commands:



rails generate devise:install
rails generate omniauth:install


Enter fullscreen mode Exit fullscreen mode

These commands will create the config/initializers/devise.rb and config/initializers/omniauth.rb files, among others.

Step 3: Add the OmniAuth providers

Now we need to add the OmniAuth providers that we want to use in our application. For example, if we want to add Google and Facebook login, we would add these lines to the config/initializers/omniauth.rb file:



Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET
  provider :facebook, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET
end


Enter fullscreen mode Exit fullscreen mode

Make sure to replace GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, FACEBOOK_APP_ID, and FACEBOOK_APP_SECRET with the appropriate keys and secrets for your application, which you can obtain from the Google and Facebook developer portals.

Step 4: Enable Omniauthable in the User model

Next, we need to add the :omniauthable module to the User model to enable social login. Assuming you are using Devise's default User model, you can do this by adding the following line to the app/models/user.rb file:



devise :omniauthable, omniauth_providers: [:google_oauth2, :facebook]


Enter fullscreen mode Exit fullscreen mode

This will enable social login for the Google and Facebook providers that we added in the previous step.

Step 5: Create a callback controller

Finally, we need to create a callback controller to handle the response from the OmniAuth provider. This is where we will process the response and create a new user if necessary. You can create a new controller with the following command:



rails generate controller users/omniauth_callbacks


Enter fullscreen mode Exit fullscreen mode

Step 6: Process the response from the OmniAuth provider

In the callback controller, add a method for each OmniAuth provider you are using. For example, to add support for Google and Facebook, you would add these methods:



class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    # Process the response from the Google OmniAuth provider
  end

  def facebook
    # Process the response from the Facebook OmniAuth provider
  end
end


Enter fullscreen mode Exit fullscreen mode

In each callback method, you will need to process the response from the OmniAuth provider and create a new user if necessary. Here is an example of how you might do this for the Google OmniAuth provider:



class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    # Get the user's information from the response
    info = request.env['omniauth.auth'].info

    # Find or create the user
    user = User.where(email: info['email']).first_or_initialize(
      email: info['email'],
      name: info['name'],
      password: Devise.friendly_token[0, 20]
    )

    # Sign in the user
    sign_in_and_redirect user, event: :authentication
  end
end


Enter fullscreen mode Exit fullscreen mode

This code will get the user's information from the response, find or create the user in the database, and then sign in the user. You can adapt this code for the other OmniAuth providers as needed.

And that's it! You should now have social login working in your Rails application using Devise and OmniAuth. Happy hacking :)

Top comments (0)