forked from nanodeath/SinatraHamlSignin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
64 lines (54 loc) · 1.12 KB
/
Copy pathmain.rb
File metadata and controls
64 lines (54 loc) · 1.12 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'haml'
enable :sessions
helpers do
def validate(username, password)
# Put your real validation logic here
return username == "max"
end
def is_logged_in?
session["logged_in"] == true
end
def clear_session
session.clear
end
def the_user_name
if is_logged_in?
session["username"]
else
"not logged in"
end
end
end
get '/' do
haml :index
end
get '/about' do
haml :about
end
get '/login' do
haml :login
end
post '/login' do
if(validate(params["username"], params["password"]))
puts "yay"
session["logged_in"] = true
session["username"] = params["username"]
# NOTE the right way to do messages like this is to use Rack::Flash
# https://github.com/nakajima/rack-flash
@message = "You've been logged in. Welcome back, #{params["username"]}"
haml :index
else
puts "error"
# See note above
@error_message = "Sorry, those credentials aren't valid."
haml :login
end
end
get '/logout' do
clear_session
@message = "You've been logged out."
haml :index
end