Module: SessionsAuthentication

Defined in:
lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb

Class Method Summary collapse

Instance Method Summary collapse

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_userObject



7
8
9
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 7

def current_user
  @current_user ||= (  ||  )
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 set_session_or_cookies(user,remember = false)
  if user
    @current_user = user
    if remember
      cookies.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]
    cookies.delete :user_id if cookies.signed[:user_id]
    reset_session
  end
end

#signin_from_cookiesObject



41
42
43
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 41

def 
  User.find(cookies.signed[:user_id]) if cookies.signed[:user_id]
end

#signin_from_sessionObject



37
38
39
# File 'lib/generators/fetty/authentication/templates/lib/sessions_authentication.rb', line 37

def 
  User.find(session[:user_id]) if session[:user_id]
end

#user_signed_in?Boolean

Returns:

  • (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