0

I use Sorcery gem in my Rails app. Oauth authentication is working on Google and Github services. But if user has same emails to login to Google and Github, my application ignores other attempt to login, because the used email already stored in database.

So, I need multiple login in my app through Oauth, even if emails in different services is equal. What should I do?

Art B
  • 147
  • 2
  • 13
  • 1
    Maybe this post will help http://stackoverflow.com/questions/21658999/omniauth-devise-error-validation-failed-email-has-already-been-taken – chumakoff Aug 14 '15 at 08:59
  • Thanks for help! But maybe there is any ways to solve this problem using only Sorcery gem? – Art B Aug 14 '15 at 09:50

1 Answers1

1

You can do it like this:

put it in ./app/controller/oauths_controller.rb

def callback

provider = auth_params[:provider]

if @user = login_from(provider)
  redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
else
  begin
    @user = create_from(provider)
    reset_session # protect from session fixation attack
    auto_login(@user)
    redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
  rescue
    provider_hash = sorcery_fetch_user_hash(provider)
    user_email = provider_hash[:user_info]['email']
    @user = User.find_by_email(user_email)
    @user.authentications.create!(:provider => provider, :uid => provider_hash[:uid])
    reset_session
    auto_login(@user)
    redirect_to root_path, :notice => "Logged in from #{provider.titleize}!"
  rescue
    redirect_to root_path, :alert => "Failed to login from #{provider.titleize}!"
  end
end

end

ivanz
  • 195
  • 3
  • 5