user3444350
user3444350

Reputation:

OmniAuth Facebook added to Devise

I'm using Rails 4.1 and already have devise configured but now would like to add sign up through Facebook and Twitter. Devise is fully set up and working for a user to sign up via email.

I've gone ahead and added the "omniauth-facebook" gem to my gemlist.

I've set up my api key and api secret

#config/initializers/devise.rb
config.omniauth :facebook, "API KEY", "API SECRET"

My Route file

#routes.rb
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks", :registrations => "registrations" }

My omniauth_callbacks_controller.rb

def facebook     
   @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)      
   if @user.persisted?       
    sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
    set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
  else
    session["devise.facebook_data"] = request.env["omniauth.auth"]
    redirect_to new_user_registration_url
  end
end

and my user model

def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  user = User.where(:provider => auth.provider, :uid => auth.uid).first
  if user
    return user
  else
    registered_user = User.where(:email => auth.info.email).first
    if registered_user
      return registered_user
    else
      user = User.create(name:auth.extra.raw_info.name,
                          provider:auth.provider,
                          uid:auth.uid,
                          email:auth.info.email,
                          password:Devise.friendly_token[0,20],
                        )
    end    
  end
end

I also added the link to the signup page but when i click on the button I'm getting the following error

{ error: { message: "Invalid redirect_uri: Given URL is not allowed by the Application configuration.", type: "OAuthException", code: 191 } }

Upvotes: 2

Views: 1458

Answers (1)

Tim
Tim

Reputation: 2923

It looks like Facebook is objecting to the url you've told it to redirect to after someone has logged in via Facebook. Check what you've specified in your Facebook app setup in "Manage Apps" -> Your App via your Facebook developer account. Check that one of your App Domains on the Settings->Basic tab matches the domain you want to redirect to after Facebook login. Also check that you're redirecting to a URL you've specified in "Valid OAuth redirect URIs" (if any) on the Settings -> Advanced tab.

My relevant config (with real domain name changed) is as follows:

On Settings -> Basic:

App Domains: mydomain.com

A web platform with URL: http://www.mydomain.com/

On Settings -> Advanced:

Client OAuth login: YES

App Secret Proof for Server API calls: YES (optional, but I like security and I don't think it'll exacerbate your problem - conversely, if you've got it as NO, then I don't think it'll matter if you leave it like that for the purposes of this problem)

Valid OAuth redirect URLs: https://www.mydomain.com/users/auth/facebook/callback https://staging.mydomain.com/users/auth/facebook/callback http://dev.mydomain.com:3000/users/auth/facebook/callback

So you can see I allow redirects to production, staging and dev environments. Your paths might vary depending on how you've set up your routes.

Then, I add an alias for the dev domain to my /etc/hosts file:

127.0.0.1 localhost dev.mydomain.com

so when Facebook tells my browser to redirect to dev.mydomain.com, it goes to the rails app on my machine.

If you specify the redirect urls, you should double-check that you're definitely supplying one of them to Facebook when you send the user there when they click on the button (I found devise/omniauth needed a bit of bludgeoning to get the paths as I wanted).

Upvotes: 4

Related Questions