Codebase list ruby-omniauth-facebook / 4f6e406
refactor example app (remove canvas example) Mark Dodwell authored 10 years ago Josef Šimánek committed 9 years ago
5 changed file(s) with 113 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
55 .powenv
66 tmp
77 bin
8 example/app.log
00 source 'https://rubygems.org'
11
22 gem 'sinatra'
3 gem 'sinatra-reloader'
34 gem 'omniauth-facebook', :path => '../'
66 GEM
77 remote: https://rubygems.org/
88 specs:
9 backports (3.3.5)
910 faraday (0.8.8)
1011 multipart-post (~> 1.2.0)
1112 hashie (2.0.5)
2930 rack (1.5.2)
3031 rack-protection (1.5.1)
3132 rack
33 rack-test (0.6.2)
34 rack (>= 1.0)
3235 sinatra (1.4.4)
3336 rack (~> 1.4)
3437 rack-protection (~> 1.4)
3538 tilt (~> 1.3, >= 1.3.4)
39 sinatra-contrib (1.4.2)
40 backports (>= 2.0)
41 multi_json
42 rack-protection
43 rack-test
44 sinatra (~> 1.4.0)
45 tilt (~> 1.3)
46 sinatra-reloader (1.0)
47 sinatra-contrib
3648 tilt (1.4.1)
3749
3850 PLATFORMS
4153 DEPENDENCIES
4254 omniauth-facebook!
4355 sinatra
56 sinatra-reloader
0 require 'sinatra'
1 require "sinatra/reloader"
2 require 'yaml'
3
4 # configure sinatra
5 set :run, false
6 set :raise_errors, true
7
8 # setup logging to file
9 log = File.new("app.log", "a+")
10 $stdout.reopen(log)
11 $stderr.reopen(log)
12 $stderr.sync = true
13 $stdout.sync = true
14
15 # server-side flow
16 get '/server-side' do
17 # NOTE: You would just hit this endpoint directly from the browser in a real app. The redirect is just here to
18 # explicit declare this server-side flow.
19 redirect '/auth/facebook'
20 end
21
22 # client-side flow
23 get '/client-side' do
24 content_type 'text/html'
25 # NOTE: When you enable cookie below in the FB.init call the GET request in the FB.login callback will send a signed
26 # request in a cookie back the OmniAuth callback which will parse out the authorization code and obtain an
27 # access_token with it.
28 <<-END
29 <html>
30 <head>
31 <title>Client-side Flow Example</title>
32 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
33 </head>
34 <body>
35 <div id="fb-root"></div>
36
37 <script type="text/javascript">
38 window.fbAsyncInit = function() {
39 FB.init({
40 appId : '#{ENV['APP_ID']}',
41 status : true, // check login status
42 cookie : true, // enable cookies to allow the server to access the session
43 xfbml : true // parse XFBML
44 });
45 };
46
47 (function(d) {
48 var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
49 js = d.createElement('script'); js.id = id; js.async = true;
50 js.src = "//connect.facebook.net/en_US/all.js";
51 d.getElementsByTagName('head')[0].appendChild(js);
52 }(document));
53
54 $(function() {
55 $('a').click(function(e) {
56 e.preventDefault();
57
58 FB.login(function(response) {
59 if (response.authResponse) {
60 $('#connect').html('Connected! Hitting OmniAuth callback (GET /auth/facebook/callback)...');
61
62 // since we have cookies enabled, this request will allow omniauth to parse
63 // out the auth code from the signed request in the fbsr_XXX cookie
64 $.getJSON('/auth/facebook/callback', function(json) {
65 $('#connect').html('Connected! Callback complete.');
66 $('#results').html(JSON.stringify(json));
67 });
68 }
69 }, { scope: 'email,read_stream', state: 'abc123' });
70 });
71 });
72 </script>
73
74 <p id="connect">
75 <a href="#">Connect to FB!</a>
76 </p>
77
78 <p id="results" />
79 </body>
80 </html>
81 END
82 end
83
84 get '/auth/:provider/callback' do
85 content_type 'application/json'
86 MultiJson.encode(request.env)
87 end
88
89 get '/auth/failure' do
90 content_type 'application/json'
91 MultiJson.encode(request.env)
92 end
00 require 'bundler/setup'
1 require 'sinatra/base'
21 require 'omniauth-facebook'
2 require './app.rb'
33
4 SCOPE = 'email,read_stream'
4 use Rack::Session::Cookie, :secret => 'abc123'
55
6 class App < Sinatra::Base
7 # turn off sinatra default X-Frame-Options for FB canvas
8 set :protection, :except => :frame_options
9
10 # server-side flow
11 get '/' do
12 # NOTE: you would just hit this endpoint directly from the browser
13 # in a real app. the redirect is just here to setup the root
14 # path in this example sinatra app.
15 redirect '/auth/facebook'
16 end
17
18 # client-side flow
19 get '/client-side' do
20 content_type 'text/html'
21 # NOTE: when you enable cookie below in the FB.init call
22 # the GET request in the FB.login callback will send
23 # a signed request in a cookie back the OmniAuth callback
24 # which will parse out the authorization code and obtain
25 # the access_token. This will be the exact same access_token
26 # returned to the client in response.authResponse.accessToken.
27 <<-END
28 <html>
29 <head>
30 <title>Client-side Flow Example</title>
31 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
32 </head>
33 <body>
34 <div id="fb-root"></div>
35
36 <script type="text/javascript">
37 window.fbAsyncInit = function() {
38 FB.init({
39 appId : '#{ENV['APP_ID']}',
40 status : true, // check login status
41 cookie : true, // enable cookies to allow the server to access the session
42 xfbml : true // parse XFBML
43 });
44 };
45
46 (function(d) {
47 var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
48 js = d.createElement('script'); js.id = id; js.async = true;
49 js.src = "//connect.facebook.net/en_US/all.js";
50 d.getElementsByTagName('head')[0].appendChild(js);
51 }(document));
52
53 $(function() {
54 $('a').click(function(e) {
55 e.preventDefault();
56
57 FB.login(function(response) {
58 if (response.authResponse) {
59 $('#connect').html('Connected! Hitting OmniAuth callback (GET /auth/facebook/callback)...');
60
61 // since we have cookies enabled, this request will allow omniauth to parse
62 // out the auth code from the signed request in the fbsr_XXX cookie
63 $.getJSON('/auth/facebook/callback', function(json) {
64 $('#connect').html('Connected! Callback complete.');
65 $('#results').html(JSON.stringify(json));
66 });
67 }
68 }, { scope: '#{SCOPE}' });
69 });
70 });
71 </script>
72
73 <p id="connect">
74 <a href="#">Connect to FB</a>
75 </p>
76
77 <p id="results" />
78 </body>
79 </html>
80 END
81 end
82
83 # auth via FB canvas and signed request param
84 post '/canvas/' do
85 # we just redirect to /auth/facebook here which will parse the
86 # signed_request FB sends us, asking for auth if the user has
87 # not already granted access, or simply moving straight to the
88 # callback where they have already granted access.
89 redirect "/auth/facebook?signed_request=#{request.params['signed_request']}"
90 end
91
92 get '/auth/:provider/callback' do
93 content_type 'application/json'
94 MultiJson.encode(request.env)
95 end
96
97 get '/auth/failure' do
98 content_type 'application/json'
99 MultiJson.encode(request.env)
100 end
6 use OmniAuth::Builder do
7 provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'], :scope => 'email,read_stream'
1018 end
1029
103 use Rack::Session::Cookie
104
105 use OmniAuth::Builder do
106 provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'], :scope => SCOPE
107 end
108
109 run App.new
10 run Sinatra::Application