Sinatra Single User Simple Auth Extension
Sinatra extension with methods and routes for dealing with simple, single user authorization (username and password)
Background
This extension is based on the awesome sinatra-simple-auth gem by Vasily Polovnyov. I wanted the exact same functionality but I also wanted to be able to specify a username/email as well so I can do some custom atlassian crowd authentication. I thought someone else may find having a username handy as well so here it is. Massive thanks to Vasily as 99.9% of this plugin is his work - I just added the username field.
Installation
gem install sinatra-single-user-auth
Or via bundler:
gem 'sinatra-single-user-auth'
Usage (classic style applications)
You just need to make sure you pass the params username and password from the login form
# classic_app.rb
require 'rubygems'
require 'sinatra'
require 'sinatra/simple_auth'
enable :sessions
set :username, 'my_username' # set the username
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 # helper method
"Hello, #{app.username}"
else
"Not authorized"
end
end
Usage (modular style applications)
# modular_app.rb
require 'sinatra/base'
require 'sinatra/simple_auth'
class SinatraModularApp < Sinatra::Base
enable :sessions
register Sinatra::SimpleAuth
set :username, 'my_username' # set the username
set :password, 'my_password' # 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 # helper method
"Hello, #{app.username}"
else
"Not authorized"
end
end
end