Rahul Dess
Rahul Dess

Reputation: 2587

How to authenticate user with Omniauth-facebook in rails 4

I have started developing my rails app and an user will login using only facebook i.e. omniauth-facebook gem. But now how can i have user_signed_in? like method without using devise in my app ?

I want to authenticate user before every action in my app.

Upvotes: 0

Views: 197

Answers (1)

Uriel Hernández
Uriel Hernández

Reputation: 741

In ApplicationController:

class ApplicationController < ActionController::Base
  #Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  protected
  def authenticate!
     redirect_to root_path unless user_signed_in?
  end
  def user_signed_in?
     !!session[:user_id]
  end
  def current_user
     User.find(session[:user_id])
  end
end

By placing the methods in ApplicationController you can call them inside any of your controllers. Now, to use them in your views, you have to copy this code into your ApplicationHelper

 module ApplicationHelper
     def user_signed_in?
      !!session[:user_id]
     end
     def current_user
        User.find(session[:user_id])
     end
 end

Upvotes: 1

Related Questions