Module: Sinatra::Warden::Helpers

Defined in:
lib/sinatra_warden/sinatra.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(*args) ⇒ Object Also known as: login

Authenticate a user against defined strategies



26
27
28
# File 'lib/sinatra_warden/sinatra.rb', line 26

def authenticate(*args)
  warden.authenticate!(*args)
end

#authenticated?(scope = nil) ⇒ Boolean Also known as: logged_in?

Check the current session is authenticated to a given scope

Returns:

  • (Boolean)


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

def authenticated?(scope=nil)
  scope ? warden.authenticated?(scope) : warden.authenticated?
end

#authorize!(failure_path = nil) ⇒ Object

Require authorization for an action

Parameters:

  • path (String)

    to redirect to if user is unauthenticated



60
61
62
63
64
65
# File 'lib/sinatra_warden/sinatra.rb', line 60

def authorize!(failure_path=nil)
  unless authenticated?
    session[:return_to] = request.path if options.auth_use_referrer
    redirect(failure_path ? failure_path : options.auth_failure_path)
  end
end

#logout(scopes = nil) ⇒ Object

Terminate the current session

Parameters:

  • the (Symbol)

    session scope to terminate



34
35
36
# File 'lib/sinatra_warden/sinatra.rb', line 34

def logout(scopes=nil)
  scopes ? warden.logout(scopes) : warden.logout(warden.config.default_scope)
end

#session_info(scope = nil) ⇒ Object

Return session info

Parameters:

  • the (Symbol)

    scope to retrieve session info for



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

def session_info(scope=nil)
  scope ? warden.session(scope) : scope
end

#user(scope = nil) ⇒ Object Also known as: current_user

Access the user from the current session

Parameters:

  • the (Symbol)

    scope for the logged in user



41
42
43
# File 'lib/sinatra_warden/sinatra.rb', line 41

def user(scope=nil)
  scope ? warden.user(scope) : warden.user
end

#user=(new_user, opts = {}) ⇒ Object Also known as: current_user=

Store the logged in user in the session

Examples:

Set John as the current user

user = User.find_by_name('John')

Parameters:

  • the (Object)

    user you want to store in the session

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :scope (Symbol)

    The scope to assign the user



52
53
54
# File 'lib/sinatra_warden/sinatra.rb', line 52

def user=(new_user, opts={})
  warden.set_user(new_user, opts)
end

#wardenObject

The main accessor to the warden middleware



8
9
10
# File 'lib/sinatra_warden/sinatra.rb', line 8

def warden
  request.env['warden']
end