Search code examples
ruby-on-railsdeviseomniauth

Devise Omniauth, how to link google account


I've set up devise to work with omniauth and. This is how devise.rb looks like:

...
  config.omniauth :facebook, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET, :scope => FACEBOOK_APP_PERMISSIONS
  config.omniauth :openid, OpenID::Store::Filesystem.new('./tmp'), :name => 'yahoo', :identifier => 'yahoo.com'
  config.omniauth :openid, OpenID::Store::Filesystem.new('./tmp'), :name => 'gmail', :identifier => 'https://www.google.com/accounts/o8/id'
...

I want to link an existing account with the the above 3, with the following code from the callback controller:

...
  def callback(provider)
    if utilizator_signed_in?
      # link it's account
      if Login.link_omniauth(current_utilizator, omniauth_data)
        flash[:notice] = I18n.t "devise.omniauth_callbacks.link.success", :kind => provider
        redirect_to :root
      end
    else
      utilizator = Login.auth_with_omniauth(omniauth_data)

      if !utilizator.nil?
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => provider
        sign_in_and_redirect utilizator, :event => :authentication
      else
        utilizator = Login.register_omniauth(omniauth_data)
        flash[:notice] = I18n.t "devise.omniauth_callbacks.register.success", :kind => provider
        sign_in_and_redirect utilizator, :event => :authentication
      end
    end
  end
...

It works just great with facebook, but for google and yahoo the current user get's signed out and a new user is created.

How can I skip the user sign_out phase ?

Thank you,

Edit: I am using the latest version 3.0.rc3. The functions from Login are easy to guess: link_omniauth - will link the current logged user to a account auth_with_omniauth - will auth the user register_omniauth - will register a new user

The problem here is that utilizator_signed_in? (user_signed_in?) will return false for google when the user is signed in, I think there is a prior sign_out happening that is not happening for facebook.


Solution

  • So I finally found the answer to my question. This guy https://github.com/intridea/omniauth/issues/185 had the same issue.

    Thanks for your replies,