1

I am using devise + omniauth to enable facebook/linkedin/twitter login on my website.

I have multiple models with totally different roles and both need social logins, I have been using devise+omniauth successfully unitl now as I needed that only for one login.

However now devise gives error saying I cant use omniauthable on multiple models.

I looked around and someone was helpful enough to create this wiki explaining how to use omniauth with devise without omniauthable module.

But I think since I would want to use it for different models, I would have to use different paths for signup, which this wiki doesnt explain.

I looked into omniauth and it has some scarce details about options request_path and callback_path which seem to be relevant, but I couldn't figure out what exactly needs to be done.

Any help will be much appreciated.

EDIT This is what I have done till now.

# omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
    FACEBOOK_AUTH_REGEX = /^\/(model1|model2)\/auth\/facebook\/$/
    FACEBOOK_CALLBACK_REGEX = /^\/(model1|model2)\/auth\/facebook\/callback\/$/

    LINKEDIN_AUTH_REGEX = /^\/(model1|model2)\/auth\/linkedin\/$/
    LINKEDIN_CALLBACK_REGEX = /^\/(model1|model2)\/auth\/linkedin\/callback\/$/

    facebook_callback_path = lambda do |env|
        env["PATH_INFO"] =~ FACEBOOK_CALLBACK_REGEX
    end

    linkedin_callback_path = lambda do |env|
        env["PATH_INFO"] =~ LINKEDIN_CALLBACK_REGEX
    end

    facebook_request_path = lambda do |env|
        match_data = FACEBOOK_AUTH_REGEX.match env["PATH_INFO"]
        if match_data
            "/#{match_data[1]}/auth/facebook/callback/"
        end
    end
    linkedin_request_path = lambda do |env|
        match_data = LINKEDIN_AUTH_REGEX.match env["PATH_INFO"]
        if match_data
            "/#{match_data[1]}/auth/linkedin/callback/"
        end
    end
    provider :facebook, FACEBOOK_ID, FACEBOOK_SECRET, :callback_path => facebook_callback_path, :request_path => facebook_request_path
    provider :linkedin, LINKEDIN_ID, LINKEDIN_SECRET, :callback_path => linkedin_callback_path, :request_path => linkedin_request_path
end

#routes.rb
match "/:model_name/auth/:provider/callback/", :to => "recruiter_omniauth_callbacks#sync"

And it seems to work fine for linkedin but in facebook I get

{"error":{"message":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100}}

Not Sure why I get this, what are the two urls it says should be same, I think I am using :model_name/auth/facebook/callback/ everywhere so not sure what is the problem it has.

Any help?

nik-v
  • 753
  • 1
  • 9
  • 20

1 Answers1

0

I post an example for a similar case here with other perspective of attacking that problem, hope it helps, regards!

Community
  • 1
  • 1
Alexis
  • 4,836
  • 2
  • 21
  • 27