-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
80 lines (62 loc) · 1.46 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require 'dotenv'
Dotenv.load
require 'sinatra'
require 'sinatra/reloader'
require 'sinatra/flash'
require 'omniauth-github'
require 'omniauth-facebook'
require_relative 'models/user'
configure :development do
require 'pry'
end
configure do
enable :sessions
use OmniAuth::Builder do
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET']
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET']
end
end
def authorize!
unless signed_in?
flash[:notice] = "You need to sign in first."
redirect '/'
end
end
helpers do
def current_user
id = session['user_id']
@current_user ||= User.find_by_id(id)
end
def signed_in?
!current_user.nil?
end
end
get '/' do
erb :index
end
get '/secret_page' do
authorize!
erb :secret_page
end
get '/auth/:provider/callback' do
# This is returns a hash with all of the information sent back by the
# service (Github or Facebook)
auth = env['omniauth.auth']
# Build a hash that represents the user from the info given back from either
# Facebook or Github
user_attributes = {
uid: auth['uid'],
provider: auth['provider'],
email: auth['info']['email'],
avatar_url: auth['info']['image']
}
user = User.create(user_attributes)
# Save the id of the user that's logged in inside the session
session["user_id"] = user.id
redirect '/'
end
get '/sign_out' do
# Sign the user out by removing the id from the session
session["user_id"] = nil
redirect '/'
end