Sinatra extension with methods and routes for dealing with simple, single-password authorization
gem install sinatra-simple-auth
Or via bundler:
gem 'sinatra-simple-auth'
# classic_app.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/simple_auth'
enable :sessions
set :password, 'my_cool_password' # set the password
set :home, '/secure/' # where user should be redirected after successful authentication
get '/login/?' do
erb :login # page with auth form
end
get '/secure/' do
protected! # protected route, requires auth
erb :secure
end
get '/' do
if authorized? # helper method
"Hello, %username%"
else
"Not authorized"
end
end
# modular_app.rb
require 'sinatra/base'
require 'sinatra/simple_auth'
class SinatraModularApp < Sinatra::Base
enable :sessions
register Sinatra::SimpleAuth
set :password, 'hello' # set the password
set :home, '/' # where user should be redirected after successful authentication
get '/login/?' do
erb :login # page with auth form
end
get '/secure/' do
protected! # protected route, requires auth
erb :secure
end
get '/' do
if authorized? # helper method
"Hello, %username%"
else
"Not authorized"
end
end
end