Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect omniauth-facebook to a specific page after authentication

Newbie here , i have omniauth-facebook setup in an application , every thing working fine , my only issue i want to redirect to another page after the authentication ,but i can't quite figure it out . my first solution was to add a route to match the auth/facebook/callback to "sessions#new " it's not working . other thing i try is to add a redirect in the controller , not working also , what is the proper to redirect to a specific page ?

controller

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    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?
      sign_in_and_redirect , :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "sign in successfuly") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

routes

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

thanks for any help .

like image 290
Obed Lorisson Avatar asked Mar 23 '23 13:03

Obed Lorisson


1 Answers

When your get user by find_for_facebook_oauth method then you can login with that user and use redirect_to for your desired path. You can do something like:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    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?
      sign_in(@user)
      redirect_to desired_path, notice: 'Signed in successfully.'
      set_flash_message(:notice, :success, :kind => "sign in successfuly") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end
like image 95
Manoj Sehrawat Avatar answered Apr 24 '23 18:04

Manoj Sehrawat