Skip to content

Instantly share code, notes, and snippets.

@jtperreault
Forked from anonymous/home_controller.rb
Last active December 28, 2015 10:39
Show Gist options
  • Save jtperreault/7487449 to your computer and use it in GitHub Desktop.
Save jtperreault/7487449 to your computer and use it in GitHub Desktop.
class HomeController < ApplicationController
def index
@data = session[:omniauth_data]
end
end
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def providers
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
User.data_from_omniauth(request.env["omniauth.auth"]) # This will have a api data, for testing i am just passing the auth and also using User model
session[:omniauth_data] = user # set session data during callback
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :google_oauth2, :providers
# def failure
# end
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:google_oauth2]
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new session["devise.user_attributes"] do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def self.data_from_omniauth(data) # This is the method i am passing data
# parse data here
# return information to be passed to controller/view
print Rails.logger.info "::USER-DATA::#{data}"
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
def confirmation_required?
super && provider.blank?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment