Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 Devise Oauth for Facebook routing confusion

After following:

https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

I can sign up a user via Facebook. But I'm struggling to define my own redirects.

Obviously it's perfect that an existing user that already facebook signed up/in redirects to my app and continues on, but it's the case of the users who are newly created via facebook i'm interested in.

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

What does user.persisted mean? find_for_facebook_oauth does the same as the devise wiki page stipulates; i.e. finds the user by email and returns it if they exists, or creates a new user with a autogen password if not.

But I need it to redirect newly created users to a page where they set their password. I don't want the stub password to persist, I want users to immediately be presented with a screen to a) confirm their name and b) confirm their password.

I have such a screen implemented for people who accept invites (via https://github.com/scambra/devise_invitable/ ) that is in views/devise/invitations/edit - which would be perfectly suitable for this if it will work.

Where should I add the redirection to and what format would this sort of redirection take? I find the facebook method above quite confusing to interpret. I can't understand why it would ever redirect to a new user registration url - the user is created or exists, so what does the persists relate to?

Obviously confused, so help appreciated. :)

Thanks,

Dave

like image 207
Dave Avatar asked Feb 09 '12 13:02

Dave


1 Answers

the .persisted? method checks whether that record is saved in your database. It's the method's way of checking if the user was successfully found or created. If not, it redirects to the new_user_registration_url because sign-up was unsuccessful (allowing the user to try again).

To redirect based on whether the user is new or not, you could check the user object for some flag that indicates they're new. You mentioned password, so something like this could work (untested):

if @user.persisted?
  flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Facebook"
  if @user.password.exists?
    sign_in_and_redirect @user, :event => :authentication
  else
    sign_in @user
    redirect_to ______ #insert path to set password etc
  end
else
  session["devise.facebook_data"] = request.env["omniauth.auth"]
  redirect_to new_user_registration_url
end

an alternative (if you want to get more fine-tuned) you may want to customize after_sign_in_path_for(resource_or_scope), as described on the Devise wiki

like image 86
Matt McCormick Avatar answered Nov 20 '22 10:11

Matt McCormick