Devise, Omniauth and 'new with session'

authentication, devise, omniauth, ruby-on-rails

Solution

new_with_session is used in build_resource. This is used with registerable (user regsitration forms).

This is only useful when your Facebook/Omniauth session already exists and you want to prefill your Registration form with some data from omniauth. (assuming you didn't already create the account automatically on callback)

# Build a devise resource passing in the session. Useful to move
# temporary session data to the newly created user.
def build_resource(hash=nil)
  hash ||= params[resource_name] || {}
  self.resource = resource_class.new_with_session(hash, session)
end

Problem

I have a method in my user model (which uses devise and confirmable) called `new_with_session` as required by Omniauth + Devise (https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview): ``` def self.new_with_session(params, session) super.tap do |user| if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] || session["devise.google_data"] && session["devise.google_data"]["extra"]["raw_info"] user.email = data["email"] end end end ``` Users are allowed to sign in using either Google or Facebook, and I'm using this line to save the right `user.email`: ``` if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] || session["devise.google_data"] && session["devise.google_data"]["extra"]["raw_info"] ``` but I don't think is the right way, so... - Do you know a better way to build `user.email` than using the `||` operator? - If I want to save some more data from Google/Facebook, like the username, should I add it to my custom `new_with_session`? If so, why?

Original source