module Devise::Controllers::Helpers
def self.define_helpers(mapping) #:nodoc:
before_action :authenticate_admin! # Tell devise to use :admin map
before_action :authenticate_user! # Tell devise to use :user map
Use:
admin_session # Session data available only to the admin scope
user_session # Session data available only to the user scope
current_admin # Current signed in admin
current_user # Current signed in user
admin_signed_in? # Checks whether there is an admin signed in or not
user_signed_in? # Checks whether there is a user signed in or not
authenticate_admin! # Signs admin in or redirect
authenticate_user! # Signs user in or redirect
Generated methods:
Admin
User
Roles:
Example:
access that specific controller/action.
so you can control the scope of the user who should be signed in to
These filters should be used inside the controllers as before_actions,
Define authentication filters and accessor helpers based on mappings.
def self.define_helpers(mapping) #:nodoc: mapping = mapping.name class_eval <<-METHODS, __FILE__, __LINE__ + 1 def authenticate_#{mapping}!(opts = {}) opts[:scope] = :#{mapping} warden.authenticate!(opts) if !devise_controller? || opts.delete(:force) end def #{mapping}_signed_in? !!current_#{mapping} end def current_#{mapping} @current_#{mapping} ||= warden.authenticate(scope: :#{mapping}) end def #{mapping}_session current_#{mapping} && warden.session(:#{mapping}) end METHODS ActiveSupport.on_load(:action_controller) do if respond_to?(:helper_method) helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session" end end end