Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get extra from omniauth-facebook. #647

Closed
alain-andre opened this issue May 27, 2016 · 2 comments
Closed

Get extra from omniauth-facebook. #647

alain-andre opened this issue May 27, 2016 · 2 comments

Comments

@alain-andre
Copy link

Hi there, I'm trying to get Facebook birthday of the user identifying to save it in my User but I don't get the data.

I have set my config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], image_size: 'large', scope: 'email,user_birthday'
end

I have seen in omniauth-facebook that they collect all infos in https://github.com/mkdynamic/omniauth-facebook/blob/master/lib/omniauth/strategies/facebook.rb with this code

extra do
  hash = {}
  hash['raw_info'] = raw_info unless skip_info?
  prune! hash
end

But I never get it in request.env['omniauth.auth'] in OmniauthCallbacksController, I only get the info.

How can I get the extra ?

@alain-andre
Copy link
Author

Ok, got it to work.

I had to over write 2 functions in OmniauthCallbacksController :

# intermediary route for successful omniauth authentication. omniauth does
# not support multiple models, so we must resort to this terrible hack.
def redirect_callbacks
  # derive target redirect route from 'resource_class' param, which was set
  # before authentication.
  devise_mapping = [request.env['omniauth.params']['namespace_name'],
                    request.env['omniauth.params']['resource_class'].underscore.gsub('/', '_')].compact.join('_')
  redirect_route = "#{request.protocol}#{request.host_with_port}/#{Devise.mappings[devise_mapping.to_sym].fullpath}/#{params[:provider]}/callback"

  # preserve omniauth info for success route. ignore 'extra' in twitter
  # auth response to avoid CookieOverflow.
  session['dta.omniauth.auth'] = (params[:provider] == 'twitter') ? request.env['omniauth.auth'].except('extra') : request.env['omniauth.auth']
  session['dta.omniauth.params'] = request.env['omniauth.params']

  redirect_to redirect_route
end

protected

  # break out provider attribute assignment for easy method extension
  def assign_provider_attrs(user, auth_hash)
    user.assign_attributes({
      nickname: auth_hash['info']['nickname'],
      name:     auth_hash['info']['name'],
      image:    auth_hash['info']['image'],
      email:    auth_hash['info']['email'],
      birthday: Date.strptime(auth_hash['extra']['raw_info']['birthday'], '%m/%d/%Y'),
      gender: auth_hash['extra']['raw_info']['gender']
    })
  end

I think this line 20 should be session['dta.omniauth.auth'] = (params[:provider] == 'twitter') ? request.env['omniauth.auth'].except('extra') : request.env['omniauth.auth'] as it says it removes extra only for Twitter.

Now I'm wondering if I have a better option to include those extra informations inside my User model than over writing a protected assign_provider_attrs method. Do I ?

@zachfeldman
Copy link
Contributor

Hi there @alain-andre ,

In an effort to cleanup this project and prioritize a bit, we're marking issues that haven't had any activity in a while with a "close-in-7-days" label. If we don't hear from you in about a week, we'll be closing this issue. Obviously feel free to re-open it at any time if it's the right time or this was done in error!

If you are still having the issue (especially if it's a bug report) please refer to our new Issue Template to provide some more details to help us solve it.

Hope all is well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants