-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.rb
72 lines (59 loc) · 1.52 KB
/
server.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
require 'sinatra'
require 'active_record'
require 'omniauth'
require 'omniauth-github'
ENV['RACK_ENV'] ||= 'development'
files = []
files.concat(Dir[File.join(File.dirname(__FILE__), 'config','**','*.rb')])
files.concat(Dir[File.join(File.dirname(__FILE__), 'models','**','*.rb')])
files.each do |file|
require file
end
class App < Sinatra::Base
configure do
set :sessions, true
set :logging, true
set :public_folder, Proc.new { "./public" }
set :views, './views'
end
configure(:development) do
set :dump_errors, true
set :show_exceptions, true
end
configure(:production) do
require 'newrelic_rpm'
# Enable multiple Heroku dynos to share sessions
set :session_secret, ENV['SESSION_KEY']
end
use OmniAuth::Builder do
provider :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET']
end
get '/' do
erb :index
end
get '/auth/:name/callback' do
auth = request.env["omniauth.auth"]
user = User.find_or_initialize_by_uid(auth["uid"])
user.update_attributes!({
:name => auth["info"]["name"],
:nickname => auth["info"]["nickname"],
:email => auth["info"]["email"],
:avatar_url => auth["info"]["image"],
})
session[:user_id] = user.id
redirect '/'
end
# If user clicks "Deny this request"
get '/auth/failure' do
redirect '/'
end
get '/logout' do
session.clear
redirect '/'
end
helpers do
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
end