Module: SinatraMore::WardenHelpers

Defined in:
lib/sinatra_more/warden_plugin/warden_helpers.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_user!Object

Login the user through the specified warden strategy



9
10
11
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 9

def authenticate_user!
  warden_handler.authenticate!
end

#authenticated?(&block) ⇒ Boolean

If a block is given, only yields the content if the user is authenticated If no block is given, returns true if the user is logged in

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 25

def authenticated?(&block)
  if block_given?
    return '' unless logged_in?
    authenticated_content = capture_html(&block)
    concat_content(authenticated_content)
  else
    return logged_in?
  end
end

#current_userObject

Returns the current authenticated user



4
5
6
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 4

def current_user
  warden_handler.user
end

#logged_in?Boolean

Returns true if the user has authenticated

Returns:

  • (Boolean)


19
20
21
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 19

def logged_in?
  warden_handler.authenticated?
end

#logout_user!Object

Signs out the user



14
15
16
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 14

def logout_user!
  warden_handler.logout
end

#must_be_authorized!(failure_path = nil) ⇒ Object

Forces a user to return to a fail path unless they are authorized Used to require a user be authenticated before routing to an action



49
50
51
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 49

def must_be_authorized!(failure_path=nil)
  redirect(failure_path ? failure_path : '/') unless authenticated?
end

#unregistered?(&block) ⇒ Boolean

If a block is given, only yields the content if the user is unregistered If no block is given, returns true if the user is not logged in

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 37

def unregistered?(&block)
  if block_given?
    return '' if logged_in?
    unregistered_content = capture_html(&block)
    concat_content(unregistered_content)
  else
    return !logged_in?
  end      
end

#warden_handlerObject

Returns the raw warden authentication handler



54
55
56
# File 'lib/sinatra_more/warden_plugin/warden_helpers.rb', line 54

def warden_handler
  request.env['warden']
end