-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.rb
89 lines (77 loc) · 2.33 KB
/
login.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
81
82
83
84
85
86
87
88
require 'rubygems'
require 'sinatra'
require "sinatra/base"
require "sinatra/content_for"
require 'json'
require 'pry'
require 'omniauth'
require 'omniauth-github'
require 'omniauth-facebook'
require 'omniauth-twitter'
require 'mongo_mapper'
set :public_folder, 'public'
class User
include MongoMapper::Document
key :firstname, :type => String
key :lastname, :type => String
key :email, :type => String
key :avatar, :type => String
key :provider, :type => String
end
class SinatraApp < Sinatra::Base
helpers Sinatra::ContentFor
configure do
set :sessions, true
set :inline_templates, true
if ENV['MONGOLAB_URI']
MongoMapper.setup({'stark_prod' => {'uri' => ENV['MONGOLAB_URI']}}, 'stark_prod')
else
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "stark_dev"
end
end
use OmniAuth::Builder do
provider :github, '1a2b64272d665d8d9c8f','2bb2410262cae42599149ae0173d0997afd8d243'
provider :facebook, '888310144647900','5060d2cf464b2f2d3e71b798f00480f0'
end
get "/" do
erb :intro
end
get '/auth/:provider/callback' do
user = User.first(:email => request.env['omniauth.auth']['info']['email'])
if !user
user = User.new(
:email => request.env['omniauth.auth']['info']['email'],
:firstname => request.env['omniauth.auth']['info']['name'].split(' ').first,
:lastname => request.env['omniauth.auth']['info']['name'].split(' ').last,
:avatar => request.env['omniauth.auth']['info']['image'],
:provider => request.env['omniauth.auth']
)
user.save!
end
session[:authenticated] = user
redirect to('/start')
end
get "/start" do
if session[:authenticated]
erb :index , :locals => {:user => session[:authenticated]}
else
redirect "/", 303
end
end
get '/auth/failure' do
erb "<h1>Authentication Failed:</h1><h3>message:<h3> <pre>#{params}</pre>"
end
get '/auth/:provider/deauthorized' do
erb "#{params[:provider]} has deauthorized this app."
end
get '/protected' do
throw(:halt, [401, "Not authorized\n"]) unless session[:authenticated]
erb "<pre>#{request.env['omniauth.auth'].to_json}</pre><hr>
<a href='/logout'>Logout</a>"
end
get '/logout' do
session[:authenticated] = false
redirect '/'
end
end