kibaekr
kibaekr

Reputation: 1349

Facebook Authentication with Devise + OmniAuth-Facebook not doing anything

I've been using https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview to get facebook authentication to work. I did everything that it told me to do, but it still doesn't work. I got a Cookie overflow error initially, so after doing some googling, I added except('extra') in the omniauth_callbacks_controller.rb like below

   session["devise.facebook_data"] = request.env["omniauth.auth"].except('extra')

That got rid of the error, but it still doesn't log the user in. In fact, I don't even think it creates the user (checked with heroku rails c).

It gives me back a url with "sign_up#=" appended to the back of it. What might be the problem?

  user = User.create(name:auth.extra.raw_info.name,
                       provider:auth.provider,
                       uid:auth.uid,
                       email:auth.info.email,
                       username:auth.info.email,
                       password:Devise.friendly_token[0,20]

                       )

I added "username," as I use that in my User model, but I can't seem to think of anything else that I might need to edit/add.

Rails 3.1.3, Ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.1]

===============quick update ===========================

So it looks like I got the user to save - I can see it in the database. However, it still doesn't log the user in, and I can't log in with that created user since I only know the username and email, and not the password. In short, I got Facebook to send me the info and I was able to save it to the db, but not redirect to a logged in screen.

Upvotes: 1

Views: 2368

Answers (1)

kibaekr
kibaekr

Reputation: 1349

Okay I figured this out.

First off, I had to add the "provider" and "uid" in the User model as an attr_accessible (after generating a migration of course), like so:

attr_accessible :password, :password_confirmation, :remember_me, :username, :email,
:bio, :website_blog, :company, :name, :image, :provider, :uid

Then, in the ApplicationController.rb, I had to tweak the after_sign_in_path_for(resource) method. I am using Devise, but I changed it a while ago to redirect the user to the previous page after signing in, but I guess now with Facebook being involved, that caused an error. So I had to comment out the previous stuff, and using just the root_url, I was able to log the user in and redirect to the homepage.

def after_sign_in_path_for(resource)       
 sign_in_url = root_url
 #sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')  

  #if (request.referer == sign_in_url)
  #  super
  #else
  #  request.referer
  #end
end

Now I just need to figure out how I would redirect the user to the previous page without causing an error and it'd be perfect.

Upvotes: 1

Related Questions