Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise, Omniauth and 'new with session'

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...

  1. Do you know a better way to build user.email than using the || operator?
  2. 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?
like image 831
Matteo Pagliazzi Avatar asked May 10 '26 08:05

Matteo Pagliazzi


1 Answers

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
like image 98
namor Avatar answered May 12 '26 06:05

namor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!