Skip to content

Instantly share code, notes, and snippets.

@jhoshina
Created July 22, 2012 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhoshina/3159766 to your computer and use it in GitHub Desktop.
Save jhoshina/3159766 to your computer and use it in GitHub Desktop.
Padrino template with mongomapper and omniauth.
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!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment