Google Integration Using Devise and Omniauth In Rails App.

ruby-on-rails-logoww             Add              118003-matte-blue-and-white-square-icon-social-media-logos-google-g-logo

In this blog i’ll show how to integrate Google authentication using devise.To install devise you can refer to my blog here.

Add the gems in your gem file

gem ‘devise’
gem 'omniauth'
gem 'omniauth-google-oauth2' 

Run the “bundle install” command to install the gem.

You need two more columns to store provider type and userid given from google

rails g migration AddProviderToUsers provider:string uid:string

Runt rake db:migrate to insert the columns in users table.

Go the user model “user.rb” and add the following line

devise : omniauthable

First of all you need to create an app in google to get “Client key” and “Client Secret key”

Create an app and get the Client id and secret key.

Now you need to declare the provider name and client id and key.Go to the file config/initializers/devise.rb and the following line

require 'omniauth-google-oauth2'
config.omniauth :google_oauth2, "APP_ID", "APP_SECRET", { access_type: "offline", approval_prompt: "" }

Go to your layout file and the following block

<% if user_signed_in? %>
Signed in as <%= %>. Not you?
<%= link_to "Sign out", destroy_user_session_path,:method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or
<%= link_to "Sign in", new_user_session_path %>
<%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %>
<% end %>

Before creating the call back method change your route like below

devise_for :users, :controllers => { : omniauth_callbacks => "omniauth_callbacks" }

Create a new controller named as “omniauth_callbacks_controller.rb”.Add the following method in it.

class OmniauthCallbacksController < Devise::OmniauthCallbacksController   
def google_oauth2
    @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user)
    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect @user, :event => :authentication
      session["devise.google_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url

Add the following block in your user model.

def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
    data =
    user = User.where(:provider => access_token.provider, :uid => access_token.uid ).first
    if user
      return user
      registered_user = User.where(:email =>
      if registered_user
        return registered_user
        user = User.create(name: data["name"],
          email: data["email"],
          uid: access_token.uid ,
          password: Devise.friendly_token[0,20],

Now we are done with google integration with devise.


21 thoughts on “Google Integration Using Devise and Omniauth In Rails App.

  1. Nice reading about you

    Thanks for visiting my blog. Be in touch. Browse through the category sections,
    I feel you may find something of your interest.

  2. I used your instructions but on my development environment I get this error message “Could not authenticate you from GoogleOauth2 because invalid credentials” Do you know what I could be doing wrong?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s