Module: Sinatra::WardenPlugin::WardenHelpers

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

Instance Method Summary collapse

Instance Method Details

#authenticate_user!Object

Login the user through the specified warden strategy



10
11
12
# File 'lib/sinatra/warden_plugin/warden_helpers.rb', line 10

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)


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

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



5
6
7
# File 'lib/sinatra/warden_plugin/warden_helpers.rb', line 5

def current_user
  warden_handler.user
end

#logged_in?Boolean

Returns true if the user has authenticated

Returns:

  • (Boolean)


20
21
22
# File 'lib/sinatra/warden_plugin/warden_helpers.rb', line 20

def logged_in?
  warden_handler.authenticated?
end

#logout_user!Object

Signs out the user



15
16
17
# File 'lib/sinatra/warden_plugin/warden_helpers.rb', line 15

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



50
51
52
# File 'lib/sinatra/warden_plugin/warden_helpers.rb', line 50

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)


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

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



55
56
57
# File 'lib/sinatra/warden_plugin/warden_helpers.rb', line 55

def warden_handler
  request.env['warden']
end