mirror of
https://github.com/simi/omniauth-facebook.git
synced 2022-11-09 12:32:45 -05:00
update example app
This commit is contained in:
parent
0bb7f29e18
commit
d62c4d0f82
3 changed files with 47 additions and 57 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,4 +6,3 @@ pkg/*
|
||||||
.powenv
|
.powenv
|
||||||
tmp
|
tmp
|
||||||
bin
|
bin
|
||||||
example/app.log
|
|
||||||
|
|
|
@ -6,57 +6,57 @@ require 'yaml'
|
||||||
set :run, false
|
set :run, false
|
||||||
set :raise_errors, true
|
set :raise_errors, true
|
||||||
|
|
||||||
# setup logging to file
|
# REQUEST STEP (server-side flow)
|
||||||
log = File.new("app.log", "a+")
|
|
||||||
$stdout.reopen(log)
|
|
||||||
$stderr.reopen(log)
|
|
||||||
$stderr.sync = true
|
|
||||||
$stdout.sync = true
|
|
||||||
|
|
||||||
# server-side flow
|
|
||||||
get '/server-side' do
|
get '/server-side' do
|
||||||
# NOTE: You would just hit this endpoint directly from the browser in a real app. The redirect is just here to
|
# NOTE: You would just hit this endpoint directly from the browser in a real app. The redirect is
|
||||||
# explicit declare this server-side flow.
|
# just here to explicit declare this server-side flow.
|
||||||
redirect '/auth/facebook'
|
redirect '/auth/facebook'
|
||||||
end
|
end
|
||||||
|
|
||||||
# client-side flow
|
# REQUEST STEP (client-side flow)
|
||||||
get '/client-side' do
|
get '/client-side' do
|
||||||
content_type 'text/html'
|
content_type 'text/html'
|
||||||
# NOTE: When you enable cookie below in the FB.init call the GET request in the FB.login callback will send a signed
|
# NOTE: When you enable cookie below in the FB.init call the GET request in the FB.login callback
|
||||||
# request in a cookie back the OmniAuth callback which will parse out the authorization code and obtain an
|
# will send a signed request in a cookie back the OmniAuth callback which will parse out the
|
||||||
# access_token with it.
|
# authorization code and obtain an access_token with it.
|
||||||
<<-END
|
<<-HTML
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Client-side Flow Example</title>
|
<title>Client-side Flow Example</title>
|
||||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js" type="text/javascript"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
window.fbAsyncInit = function() {
|
||||||
|
FB.init({
|
||||||
|
appId: '#{ENV['APP_ID']}',
|
||||||
|
cookie: true // IMPORTANT must enable cookies to allow the server to access the session
|
||||||
|
});
|
||||||
|
console.log("fb init");
|
||||||
|
};
|
||||||
|
|
||||||
|
(function(d, s, id){
|
||||||
|
var js, fjs = d.getElementsByTagName(s)[0];
|
||||||
|
if (d.getElementById(id)) {return;}
|
||||||
|
js = d.createElement(s); js.id = id;
|
||||||
|
js.src = "//connect.facebook.net/en_US/sdk.js";
|
||||||
|
fjs.parentNode.insertBefore(js, fjs);
|
||||||
|
}(document, 'script', 'facebook-jssdk'));
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="fb-root"></div>
|
<div id="fb-root"></div>
|
||||||
|
|
||||||
|
<p id="connect">
|
||||||
|
<a href="#">Connect to FB!</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<p id="results" />
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
window.fbAsyncInit = function() {
|
|
||||||
FB.init({
|
|
||||||
appId : '#{ENV['APP_ID']}',
|
|
||||||
status : true, // check login status
|
|
||||||
cookie : true, // enable cookies to allow the server to access the session
|
|
||||||
xfbml : true // parse XFBML
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
(function(d) {
|
|
||||||
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
|
|
||||||
js = d.createElement('script'); js.id = id; js.async = true;
|
|
||||||
js.src = "//connect.facebook.net/en_US/all.js";
|
|
||||||
d.getElementsByTagName('head')[0].appendChild(js);
|
|
||||||
}(document));
|
|
||||||
|
|
||||||
$(function() {
|
|
||||||
$('a').click(function(e) {
|
$('a').click(function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
FB.login(function(response) {
|
FB.login(function(response) {
|
||||||
|
console.log(response);
|
||||||
if (response.authResponse) {
|
if (response.authResponse) {
|
||||||
$('#connect').html('Connected! Hitting OmniAuth callback (GET /auth/facebook/callback)...');
|
$('#connect').html('Connected! Hitting OmniAuth callback (GET /auth/facebook/callback)...');
|
||||||
|
|
||||||
|
@ -67,27 +67,18 @@ get '/client-side' do
|
||||||
$('#results').html(JSON.stringify(json));
|
$('#results').html(JSON.stringify(json));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, { scope: 'email,read_stream', state: 'abc123' });
|
}); // if you want custom scopes, pass them as an extra, final argument to FB.login
|
||||||
});
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<p id="connect">
|
|
||||||
<a href="#">Connect to FB!</a>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p id="results" />
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
END
|
HTML
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# CALLBACK STEP
|
||||||
|
# - redirected here for server-side flow
|
||||||
|
# - ajax request made here for client-side flow
|
||||||
get '/auth/:provider/callback' do
|
get '/auth/:provider/callback' do
|
||||||
content_type 'application/json'
|
content_type 'application/json'
|
||||||
MultiJson.encode(request.env)
|
MultiJson.encode(request.env)
|
||||||
end
|
end
|
||||||
|
|
||||||
get '/auth/failure' do
|
|
||||||
content_type 'application/json'
|
|
||||||
MultiJson.encode(request.env)
|
|
||||||
end
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ require './app.rb'
|
||||||
use Rack::Session::Cookie, :secret => 'abc123'
|
use Rack::Session::Cookie, :secret => 'abc123'
|
||||||
|
|
||||||
use OmniAuth::Builder do
|
use OmniAuth::Builder do
|
||||||
provider :facebook, ENV['APP_ID'], ENV['APP_SECRET'], :scope => 'email,read_stream'
|
provider :facebook, ENV['APP_ID'], ENV['APP_SECRET']
|
||||||
end
|
end
|
||||||
|
|
||||||
run Sinatra::Application
|
run Sinatra::Application
|
||||||
|
|
Loading…
Reference in a new issue