Skip to content

Instantly share code, notes, and snippets.

@itolosa
Created August 29, 2018 02:21
Show Gist options
  • Save itolosa/9105ccedde96b1763beee50d5daaf167 to your computer and use it in GitHub Desktop.
Save itolosa/9105ccedde96b1763beee50d5daaf167 to your computer and use it in GitHub Desktop.
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
#byebug
auth = request.env['omniauth.auth']
@identity = Identity.find_or_create_with_omniauth(auth)
if user_signed_in?
if @identity.user == current_user
redirect_to root_url, notice: "Already linked that account!"
elsif @identity.user.present?
raise "error: identity is already used by another user"
else
@identity.user = current_user
@identity.save
redirect_to root_url, notice: "Successfully linked that account!"
end
else
user = User.create_with_omniauth(auth)
if user.persisted?
unless @identity.user.present?
@identity.user = user
@identity.save
end
sign_in_and_redirect @identity.user, event: :authentication
if is_navigational_format?
set_flash_message(:notice, :success, kind: auth['provider'].capitalize)
end
else
session['devise.user_attributes'] = user.attributes
session['devise.identity_attributes'] = auth.slice('provider', 'uid')
redirect_to new_user_registration_url, notice: "Please finish registering"
end
end
end
Devise.omniauth_providers.each do |provider|
alias_method provider, :all
end
def failure
redirect_to root_url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment