5 Matching Annotations
- Apr 2021
-
github.com github.com
-
# unauthenticated do # as :user do # root to: 'devise/registrations#new' # end # end
-
# authenticated :user, lambda {|u| u.role == "admin"} do # root to: "admin/dashboard#show", as: :user_root # end
-
-
github.com github.com
-
class LoggedInConstraint def self.matches?(request) request.env['warden'].authenticate? end end
(have not tried)
-
-
stackoverflow.com stackoverflow.com
-
class AuthConstraint def initialize(&block) @block = block || ->(_) { true } end def matches?(req) user = current_user(req) user.present? && @block.call(user) end def current_user(req) User.find_by_id(session[:user_id]) end end This is a flexible approach to defining route access based on any desired variable (roles, auth, etc...)
Good solution, and might be needed if you want to base routes on roles, etc. — but this one is even easier if all you need is for it to be conditional based on signed in or not (because devise provides authenticated helper):
-
-
stackoverflow.com stackoverflow.com
-
authenticated :user do root 'calendars#index', as: :authenticated_root end unauthenticated :user do root 'pages#home', as: :unauthenticated_root end
-