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



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sinatra_warden/sinatra.rb', line 67

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

  # Enable Sessions
  app.set :sessions, true

  app.set :auth_failure_path, '/'
  app.set :auth_success_path, '/'
  # Setting this to true will store last request URL
  # into a user's session so that to redirect back to it
  # upon successful authentication
  app.set :auth_use_referrer, false

  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
    warden.custom_failure! if warden.config.failure_app == self.class
    env['x-rack.flash'][:error] = 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
      authenticate
      env['x-rack.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
    authenticate
    env['x-rack.flash'][:success] = options.auth_success_message if defined?(Rack::Flash)
    redirect options.auth_use_referrer && session[:return_to] ? session.delete(:return_to) : 
             options.auth_success_path
  end

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