Module: SessionsAuthentication
- Defined in:
- lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate_user! ⇒ Object
- #current_user ⇒ Object
- #redirect_to_target_or_default_url(default = root_url) ⇒ Object
- #set_session_or_cookies(user, remember = false) ⇒ Object
- #signin_from_cookies ⇒ Object
- #signin_from_session ⇒ Object
- #user_signed_in? ⇒ Boolean
Class Method Details
.included(receiver) ⇒ Object
3 4 5 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 3 def self.included(receiver) receiver.send :helper_method, :current_user, :user_signed_in? end |
Instance Method Details
#authenticate_user! ⇒ Object
15 16 17 18 19 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 15 def authenticate_user! unless user_signed_in? redirect_to new_session_url, :alert => "You must sign in first before accessing this page." end end |
#current_user ⇒ Object
7 8 9 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 7 def current_user @current_user ||= ( signin_from_session || ) end |
#redirect_to_target_or_default_url(default = root_url) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 45 def redirect_to_target_or_default_url(default = root_url) if request.env["HTTP_REFERER"].blank? || request.env["HTTP_REFERER"] == new_session_url default else request.env["HTTP_REFERER"] end end |
#set_session_or_cookies(user, remember = false) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 21 def (user,remember = false) if user @current_user = user if remember .permanent.signed[:user_id] = user.id else session[:user_id] = user.id end else @current_user = nil session[:user_id] = nil if session[:user_id] .delete :user_id if .signed[:user_id] reset_session end end |
#signin_from_cookies ⇒ Object
41 42 43 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 41 def User.find(.signed[:user_id]) if .signed[:user_id] end |
#signin_from_session ⇒ Object
37 38 39 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 37 def signin_from_session User.find(session[:user_id]) if session[:user_id] end |
#user_signed_in? ⇒ Boolean
11 12 13 |
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 11 def user_signed_in? current_user ? true : false end |