forked from gmanley/sinatra_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
54 lines (45 loc) · 1.01 KB
/
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
47
48
49
50
51
52
53
54
class App < Sinatra::Base
extend Settings
configure(:development) do |c|
register Sinatra::Reloader
c.also_reload('lib/**/*.rb')
end
configure do
set :root, APP_ROOT
set :haml, {format: :html5}
enable(:sessions)
set :session_secret, config.session_secret
use Rack::Flash, sweep: true
end
helpers(ApplicationHelper)
get '/' do
haml :index
end
post '/user/login' do
if user = User.authenticate(params[:email], params[:password])
flash[:notice] = "Logged in successfully"
session[:user_id] = user.id.to_s
redirect '/'
else
flash[:notice] = "Incorrect credentials"
redirect '/'
end
end
get '/user/new' do
haml :register
end
post '/user/new' do
@user = User.create(params)
if @user.valid?
flash[:notice] = "Registered successfully!"
session[:user_id] = @user.id.to_s
redirect '/'
else
haml :register
end
end
get '/user/logout' do
session[:user_id] = nil
redirect '/'
end
end