forked from bootstrap-ruby/sinatra-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
46 lines (36 loc) · 951 Bytes
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'rubygems'
require 'sinatra'
configure do
set :public_folder, Proc.new { File.join(root, "static") }
enable :sessions
end
helpers do
def username
session[:identity] ? session[:identity] : 'Hello stranger'
end
end
before '/secure/*' do
if !session[:identity] then
session[:previous_url] = request.path
@error = 'Sorry guacamole, you need to be logged in to visit ' + request.path
halt erb(:login_form)
end
end
get '/' do
erb 'Can you handle a <a href="/secure/place">secret</a>?'
end
get '/login/form' do
erb :login_form
end
post '/login/attempt' do
session[:identity] = params['username']
where_user_came_from = session[:previous_url] || '/'
redirect to where_user_came_from
end
get '/logout' do
session.delete(:identity)
erb "<div class='alert alert-message'>Logged out</div>"
end
get '/secure/place' do
erb "This is a secret place that only <%=session[:identity]%> has access to!"
end