Module: Sinatra::Warden

Defined in:
lib/sinatra_warden/sinatra.rb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sinatra_warden/sinatra.rb', line 39

def self.registered(app)
  app.helpers Warden::Helpers

  # Enable Sessions
  app.set :sessions, true

  app.set :auth_failure_path, '/'
  app.set :auth_success_path, '/'

  app.set :auth_error_message,   "Could not log you in."
  app.set :auth_success_message, "You have logged in successfully."
  app.set :auth_use_erb, false
  app.set :auth_login_template, :login
  
  # OAuth Specific Settings
  app.set :auth_use_oauth, false

  app.post '/unauthenticated/?' do
    status 401
    flash[:error] = (env['warden'].message || options.auth_error_message) if defined?(Rack::Flash)
    options.auth_use_erb ? erb(options.) : haml(options.)
  end

  app.get '/login/?' do
    if options.auth_use_oauth && !@auth_oauth_request_token.nil?
      session[:request_token] = @auth_oauth_request_token.token
      session[:request_token_secret] = @auth_oauth_request_token.secret
      redirect @auth_oauth_request_token.authorize_url
    else          
      options.auth_use_erb ? erb(options.) : haml(options.)
    end
  end

  app.get '/oauth_callback/?' do
    if options.auth_use_oauth
      env['warden'].authenticate!
      flash[:success] = options.auth_success_message if defined?(Rack::Flash)
      redirect options.auth_success_path
    else
      redirect options.auth_failure_path
    end
  end

  app.post '/login/?' do
    env['warden'].authenticate!
    flash[:success] = options.auth_success_message if defined?(Rack::Flash)
    redirect options.auth_success_path
  end

  app.get '/logout/?' do
    authorize!
    env['warden'].logout(:default)
    flash[:success] = options.auth_success_message if defined?(Rack::Flash)
    redirect options.auth_success_path
  end
end