Created
October 18, 2012 00:14
-
-
Save h5y1m141/3909148 to your computer and use it in GitHub Desktop.
SinatraとOmniAuthでFacebookアカウント利用したログイン&ログアウトのサンプル
This file contains 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 'haml' | |
require 'omniauth' | |
require 'omniauth-facebook' | |
require 'logger' | |
class MyApp < Sinatra::Base | |
SCOPE = 'email,read_stream' | |
ENV['APP_ID'] = "APPID" | |
ENV['APP_SECRET'] = "APPSECRET" | |
log = Logger.new('app.log') | |
use OmniAuth::Builder do | |
provider :facebook, ENV['APP_ID'],ENV['APP_SECRET'], :scope => SCOPE | |
end | |
set :sessions, true | |
use Rack::Session::Cookie | |
get '/' do | |
if session[:uid] == nil | |
redirect '/auth/facebook' | |
else | |
haml :main | |
end | |
end | |
get '/logout' do | |
session.clear | |
haml :logout | |
end | |
get '/auth/:provider/callback' do | |
info = request.env['omniauth.auth'] | |
log.info('finish authenticate!') | |
session[:uid] = info["uid"] | |
session[:user_name] = info["info"]["name"] | |
session[:image]= info["info"]["image"] | |
redirect '/' | |
end | |
get '/auth/failure' do | |
@message = params | |
haml :fail | |
end | |
end | |
MyApp.run! :host => 'localhost', :port => 4567 |
This file contains 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
source :rubyforge | |
gem 'sinatra' | |
gem 'omniauth' | |
gem 'omniauth-facebook' | |
gem 'haml' |
This file contains 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
!!! 5 | |
%head | |
%body | |
%p= 'Logout success.' | |
%a{:href => '/'} Return main page |
This file contains 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
!!! 5 | |
%head | |
%body | |
%h1= "Welcome! user is #{session[:user_name]}" | |
%img{:src => session[:image]} | |
%p | |
%a{:href => '/logout'} logout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment