Skip to content

Instantly share code, notes, and snippets.

@aneziocampos
Created October 11, 2012 12:48
Show Gist options
  • Save aneziocampos/3872067 to your computer and use it in GitHub Desktop.
Save aneziocampos/3872067 to your computer and use it in GitHub Desktop.
class AuthorizationsController < ApplicationController
before_filter :require_user, :only => [:destroy]
def create
omniauth = request.env['omniauth.auth'] #this is where you get all the data from your provider through omniauth
@auth = Authorization.find_from_hash(omniauth)
if omniauth['provider'] == 'stripe_platform'
current_user.stripe_token = omniauth[:credentials][:token]
current_user.stripe_publishable_key = omniauth[:extra][:raw_info][:stripe_publishable_key]
current_user.save
redirect_to stripe_splash_path(omniauth: omniauth)
else
if current_user
#flash[:notice] = "Successfully added #{omniauth['provider']} authentication"
#Add an auth to existing user
current_user.authorizations.create(:provider => omniauth['provider'], :uid => omniauth['uid'])
session[:fbtoken] = omniauth['credentials']['token']
elsif @auth
#flash[:notice] = "Welcome back #{omniauth['provider']} user"
#User is present. Login the user with his social account
UserSession.create(@auth.user, true)
session[:fbtoken] = omniauth['credentials']['token']
else
@new_auth = Authorization.create_from_hash(omniauth, current_user) #Create a new user
#flash[:notice] = "Welcome #{omniauth['provider']} user. Your account has been created."
#Log the authorizing user in.
UserSession.create(@new_auth.user, true)
session[:fbtoken] = omniauth['credentials']['token']
end
if !session[:original_uri].nil?
uri = session[:original_uri]
session[:original_uri] = nil
redirect_to uri and return
end
redirect_to root_url
end
end
def failure
flash[:notice] = "Sorry, You din't authorize"
redirect_to root_url
end
def destroy
@authorization = current_user.authorizations.find(params[:id])
flash[:notice] = "Successfully deleted #{@authorization.provider} authentication."
@authorization.destroy
redirect_to root_url
end
def stripe_splash
@omniauth = params[:omniauth]
end
end
Rails.application.config.middleware.use OmniAuth::Builder do
if Rails.env.production?
provider :twitter, 'xxx', 'xxx'
provider :facebook, ENV['FACEBOOK_ID'], ENV['FACEBOOK_SECRET'], {:scope => 'publish_stream,offline_access,email'}
provider :linked_in, 'xxx', 'xxx'
elsif Rails.env.development?
provider :facebook, 'xxx', 'xxx', {:scope => 'publish_stream,offline_access,email'}
else
provider :facebook, 'xxx', 'xxx', {:scope => 'publish_stream,offline_access,email'}
end
provider :stripe_platform, 'ca_0TouMzxxx', 'sk_1yZsxxx', {:scope => 'read_write'}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment