-
Notifications
You must be signed in to change notification settings - Fork 0
/
chitter.rb
75 lines (59 loc) · 1.23 KB
/
chitter.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require 'sinatra/base'
require_relative 'lib/config'
require_relative 'lib/store'
require_relative 'lib/user'
require_relative 'lib/peep'
CONFIG_PATH = '/Users/jaetimothysalva/projects/chitter/config.json'
config = Config.new(CONFIG_PATH)
config.init_prod_credentials
Store.init config.prod_project_id
class Chitter < Sinatra::Base
enable :sessions
get '/' do
@user = session[:user]
p @peeps = Peep.all
erb :index
end
post '/new-peep' do
user = session[:user]
if user.nil?
redirect '/sign-in'
end
new_peep = Peep.new(text: params[:text], author: user.username)
Peep.add(new_peep)
redirect '/'
end
get '/sign-in' do
unless session[:user].nil?
redirect '/'
end
erb :sign_in
end
post '/sign-in' do
user = User.authenticate(params[:username], params[:password])
if user
session[:user] = user
redirect '/'
else
redirect '/sign-in'
end
end
get '/sign-out' do
session[:user] = nil
redirect '/'
end
get '/register' do
erb :register
end
post '/register' do
new_user = User.new(
username: params[:username],
name: params[:name],
email: params[:email]
)
User.add(new_user)
User.set_password(new_user, params[:password])
session[:user] = new_user
redirect '/'
end
end