ac360
ac360

Reputation: 7835

Authentication w/ Omniauth (No Devise) - How to Check is Signed In in Routes?

I'm using only omniauth-oauth2 for authentication (No Devise). I would like to define different roots for users who are signed in and are not, to include a secure dashboard. I have a current_user method in my Application Controller, but I can't seem to access it in my routes. Does anyone have any recommendations as to how I can best do this?

Application Controller:

helper_method :current_user

private

    def current_user
        @current_user ||= User.find(session[:user_id]) if session[:user_id]
    end

This does not work:

Blog::Application.routes.draw do


 if :current_user
   root :to => "home#show"
 else
   root :to => "home#index"
 end

end

Upvotes: 0

Views: 266

Answers (1)

Sagar Bommidi
Sagar Bommidi

Reputation: 1409

in routes.rb file, just specify the constraint for routing based on condition.

root :to => 'home#show', :constraints => lambda{|req| !req.session[:user_id].blank?}
root :to => 'home#index'

Hope this will useful as you are storing the user_id in the session, this will work

Upvotes: 1

Related Questions