とりあえずできた。
https://gist.github.com/3159766
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
project :orm => :mongomapper, :renderer => :erb | |
require_dependencies 'omniauth', 'omniauth-facebook' | |
run_bundler | |
generate :model, 'account name:string role:string uid:string uid:string provider:string' | |
account_partial = <<-RUBY | |
def self.create_with_omniauth(auth) | |
create!(provider: auth['provider'], | |
uid: auth['uid'], | |
name: auth['name'], | |
role: 'users' | |
) | |
end | |
RUBY | |
inject_into_file 'models/account.rb', account_partial, :after => "timestamps!\n" | |
access_control = <<-RUBY | |
register Padrino::Admin::AccessControl | |
use OmniAuth::Builder do | |
provider :facebook, ENV['FB_APP_ID'], ENV['FB_APP_SECRET'] | |
end | |
set :login_page, '/' | |
access_control.roles_for :any do |role| | |
role.protect '/profile' | |
role.protect 'admin' | |
end | |
access_control.roles_for :users do |role| | |
role.allow '/profile' | |
end | |
get :index do | |
render :index | |
end | |
get :profile do | |
content_type :text | |
current_account.to_yaml | |
end | |
get :destroy do | |
set_current_account(nil) | |
redirect url(:index) | |
end | |
get :auth, :map => '/auth/:provider/callback' do | |
auth = request.env['omniauth.auth'] | |
account = Account.find_by_provider_and_uid(auth['provider'], auth['uid']) || | |
Account.create_with_omniauth(auth) | |
set_current_account(account) | |
redirect 'http://' + request.env['HTTP_HOST'] + url(:profile) | |
end | |
RUBY | |
inject_into_file 'app/app.rb', access_control, :after => "enable :sessions\n" | |
index_file = <<-ERB | |
<%= link_to 'Login with Facebook', '/auth/facebook' %> | |
<%= link_to 'Logout', '/destroy' %> | |
ERB | |
create_file 'app/views/index.erb', index_file | |
#ignore_openssl_verify = <<-RUBY | |
#OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE | |
# | |
#RUBY | |
#inject_into_file 'config/boot.rb', ignore_openssl_verify, :before => "Padrino.load!" |
$ padrino g project hoge -p https://gist.github.com/3159766
みたいにするとFacebook認証する空アプリができる。
FB_APP_IDとFB_APP_SECRETを環境変数に登録しないといけない。powでやるなら簡単だね。
でも、自分の環境だとOpenSSLのverify errorとかなんとかがでる。
$ ruby -ropenssl -e 'p OpenSSL::X509::DEFAULT_CERT_FILE'
"/Users/jhoshina/.rvm/usr/ssl/cert.pem"
あー、rvm pkgで入れたやつか。空になってる。どっかからcert.pemをパクってきて上書いたら通った。
これで、認証をfacebookとかにぶん投げることができて、アプリ作りに専念できる…かな?