Search code examples
ruby-on-railsfacebookruby-on-rails-4omniauthfacebook-oauth

Redirect to Page after Facebook Sign Up


I'm trying to Redirect a user after successful Facebook SIGN UP (not sign in).

I want to redirect to /getstarted/welcome after a user Registers for the first time.

My omniauth callback is :

def facebook
        # You need to implement the method below in your model (e.g. app/models/user.rb)
        @user ||=
            User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

        if @user.persisted?
            # This will throw if @user is not activated
            sign_in_and_redirect @user, event: :authentication
            if is_navigational_format?
                set_flash_message(:notice, :success, kind: "Facebook")
            end
        else
            session["devise.facebook_data"] = request.env["omniauth.auth"]
            redirect_to new_user_registration_url
        end
    end

For devise i use

def after_sign_up_path_for(source)
  '/getstarted/welcome'
end

My User Model:

Facebook Settings

  def self.find_for_facebook_oauth(auth, signed_in_resource = nil)
    user = User.where(provider: auth.provider, uid: auth.uid).first
    if user.present?
        user
    else
        user = User.create(first_name:auth.extra.raw_info.first_name,
                                             last_name:auth.extra.raw_info.last_name,
                                             facebook_link:auth.extra.raw_info.link,
                                             user_name:auth.extra.raw_info.name,
                                             provider:auth.provider,
                                             uid:auth.uid,
                                             email:auth.info.email,
                                             password:Devise.friendly_token[0,20])
    end
end

Can someone help me set this up ?


Solution

  • I solved it by adding to my User model

    attr_accessor `just_signed_up`
    

    and setting it in User.find_for_facebook_oauth in this part of the block where I create a new user (first_or_create block).

    EDIT: more explanation

    So in Ruby (not Rails) there is a class method/macro called attr_accessor (actually theres also attr_reader and attr_writer, attr_accessor is a shorthand for calling the other two)

    If you, in your User model write

    class User
      attr_accessor :some_attribute
    

    then you're able to perform

    u = User.first
    u.some_attribute = 'asdf'
    u.some_attribute # => 'asdf'
    

    but this attribute is not going to be saved to DB, so it may be used as a temporary storage of some value in Rails model.

    Another thing to know is that there are only two "values" that are false in Ruby, those are false and nil.

    Using those two tricks you may create a new user and temporarily set this flag on the object

    u = User.create u.just_signed_up = true u.just_signed_up # => true u.reload! # fetches record from DB u.just_signed_up # => nil

    and since nil is false, this check will fail for every user except for ones you just created!