Proper way to handle new user registration through OmniAuth?

213 views
Skip to first unread message

S Wrobel

unread,
Dec 31, 2010, 10:50:48 PM12/31/10
to Devise
Following the Wiki entry @ https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
seems to provide some conflicting information.

It recommends adding the following to the User model:
def self.find_for_facebook_oauth(access_token,
signed_in_resource=nil)
data = access_token['extra']['user_hash']
if user = User.find_by_email(data["email"])
user
else # Create an user with a stub password.
User.create!(:email => data["email"], :password =>
Devise.friendly_token[0,20])
end
end

As well as overriding the new_with_session method as follows:
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] &&
session["devise.facebook_data"]["extra"]["user_hash"]
user.email = data["email"]
end
end
end

However the new _with_session method doesn't seem to actually get
called when creating a new user via facebook since it just goes
straight to User.create!

I added code to new_with_session to grab some session data and attach
it to the new user but it only seems to work when creating a new user
through the form rather than through Facebook. What's the best
practice so Omniauth registration also calls new_with_session?

iwar...@gmail.com

unread,
Jan 4, 2011, 3:21:08 PM1/4/11
to Devise
I am curious about this as well.

It doesn't seem to be necessary as far as I can tell. The standard
RegistrationsController calls build_resource, which calls
resource.new_with_session, but like you said,
OmniauthCallbacksController calls User.find_for_facebook_oauth which
creates a new user.

ian


On Dec 31 2010, 9:50 pm, S Wrobel <swro...@gmail.com> wrote:
> Following the Wiki entry @https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

Kyle Mulka

unread,
Feb 17, 2011, 5:51:25 PM2/17/11
to Devise
I've got the same issue. Could someone explain how to get
new_with_session called, or maybe remove it from the tutorial?

Thanks,

Kyle

On Jan 4, 3:21 pm, "iwars...@stripey.net" <iwars...@gmail.com> wrote:
> I am curious about this as well.
>
> It doesn't seem to be necessary as far as I can tell. The standard
> RegistrationsController calls build_resource, which calls
> resource.new_with_session, but like you said,
> OmniauthCallbacksController calls User.find_for_facebook_oauth which
> creates a new user.
>
> ian
>
> On Dec 31 2010, 9:50 pm, S Wrobel <swro...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Following the Wiki entry @https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
> > seems to provide some conflicting information.
>
> > It recommends adding the following to the User model:
> >  def self.find_for_facebook_oauth(access_token,
> > signed_in_resource=nil)
> >   data = access_token['extra']['user_hash']
> >   if user = User.find_by_email(data["email"])
> >     user
> >   else # Create an user with a stub password.
> >     User.create!(:email => data["email"], :password =>
> > Devise.friendly_token[0,20])
> >   end
> > end
>
> > As well as overriding thenew_with_sessionmethod as follows:
> >   def self.new_with_session(params, session)
> >     super.tap do |user|
> >       if data = session["devise.facebook_data"] &&
> > session["devise.facebook_data"]["extra"]["user_hash"]
> >         user.email = data["email"]
> >       end
> >     end
> >   end
>
> > However the new _with_session method doesn't seem to actually get
> > called when creating a new user via facebook since it just goes
> > straight to User.create!
>
> > I added code tonew_with_sessionto grab some session data and attach
Reply all
Reply to author
Forward
0 new messages