-
Notifications
You must be signed in to change notification settings - Fork 108
/
app.rb
43 lines (35 loc) · 874 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
require 'rubygems'
require 'sinatra'
configure do
enable :sessions
end
helpers do
def username
session[:identity] ? session[:identity] : 'Hello stranger'
end
end
before '/secure/*' do
unless session[:identity]
session[:previous_url] = request.path
@error = 'Sorry, 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