Created
September 12, 2012 14:53
-
-
Save codeincontext/3707167 to your computer and use it in GitHub Desktop.
Facebook and Twitter login with Sinatra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sinatra' | |
require 'json' | |
require 'omniauth' | |
require 'omniauth-facebook' | |
require 'omniauth-twitter' | |
class SinatraApp < Sinatra::Base | |
configure do | |
set :sessions, true | |
set :inline_templates, true | |
end | |
use OmniAuth::Builder do | |
provider :facebook, '290594154312564','a26bcf9d7e254db82566f31c9d72c94e' | |
provider :twitter, 'cO23zABqRXQpkmAXa8MRw', 'TwtroETQ6sEDWW8HEgt0CUWxTavwFcMgAwqHdb0k1M' | |
end | |
get '/' do | |
erb " | |
<a href='http://localhost:4567/auth/facebook'>Login with facebook</a><br> | |
<a href='http://localhost:4567/auth/twitter'>Login with twitter</a><br>" | |
end | |
get '/auth/:provider/callback' do | |
uid = request.env['omniauth.auth']['uid'] | |
puts uid | |
erb "<h1>#{params[:provider]}</h1> | |
<pre>#{JSON.pretty_generate(request.env['omniauth.auth'])}</pre>" | |
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 | |
SinatraApp.run! if __FILE__ == $0 | |
__END__ | |
@@ layout | |
<html> | |
<head> | |
<link href='http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css' rel='stylesheet' /> | |
</head> | |
<body> | |
<div class='container'> | |
<div class='content'> | |
<%= yield %> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment