Integrating Omniauth for Github


A good news for you! Our Github Editor is on its  way! In Today’s post, I am going to discuss integrating omniauth for Github using Devise gem in Ruby On Rails.

Omniauth is basically a library that standardizes the authentication for our web application. You might have seen on the various application while you are log in there is a special feature which allows you to login in from Google or from Facebook or GitHub!. You don’t have to sign up again and again just click on it and log in from somewhere else.

So Here I am going to tell you about. How to integrate it in Ruby On Rails application.


Before moving on to Omni auth, You have to integrate Devise Authentication in your app

Simple Steps to integrate omniauth in Ruby On Rails app
First Step is to add gem in gemfile
gem 'omniauth-github'

Here we will use github but you can add omniauth for other apps also like facebook or google just replace github with any of this Stratergies

Next up, you should add the columns “provider” (string) and “uid” (string) to your User model.

rails g migration AddOmniauthToUsers provider:string uid:string
rake db:migrate

Next, Declare the provider in your


config.omniauth :github, "APP_ID", "APP_SECRET", scope: 'user,public_repo

and replace APP_ID and APP_SECRET with your app id and secret.

You can generate App ID and secret from your Github account

After configuring your strategy, you need to make your model


devise :omniauthable, :omniauth_providers => [:github]

Then add,

Then update your routes in config/routes.rb,
devise_for :users, :controllers => { :omniauth_callbacks =>"users/omniauth_callbacks" }

Now add the file


class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

The callback should be implemented as an action with the same name as the provider like this,

class Users::OmniauthCallbacksController :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Github") if is_navigational_format?
session["devise.github_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url

def failure
redirect_to root_path

This action has a few aspects worth describing:

When a valid user is found, they can be signed in with one of two Devise methods: sign_in or sign_in_and_redirect. Passing :event => :authentication is optional. You should only do so if you wish to use Warden callbacks.

After the controller is defined, we need to implement the from_omniauth method in our model



def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user| =
user.password = Devise.friendly_token[0,20] = # assuming the user model has a name
user.image = # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!

This method tries to find an existing user by the provider and uid fields. If no user is found, a new one is created with a random password and some extra information.

Notice that Devise’s RegistrationsController by default calls User.new_with_session before building a resource. This means that, if we need to copy data from session whenever a user is initialized before sign up, we just need to implement new_with_session in our model. Here is an example that copies the github email if available

class User < ApplicationRecord
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] = data["email"] if

For more infor you can go to Documentation
 And here you are done. 🙂
Leave Comments if you are getting any trouble.
I am always ready for help!
Keep Rocking!


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s