Skip to content

Instantly share code, notes, and snippets.

@dennismonsewicz
Last active December 21, 2015 11:29
Show Gist options
  • Save dennismonsewicz/72fcdc04eeb545b0b473 to your computer and use it in GitHub Desktop.
Save dennismonsewicz/72fcdc04eeb545b0b473 to your computer and use it in GitHub Desktop.
class AuthorizationsController < ApplicationController
def create
$redis.set("omniauth_data", request.env["omniauth.auth"].to_json)
authentication = Authorization.find_by_provider_and_uid(auth['provider'], auth['uid'])
if authentication
sign_in :user, authentication.user
redirect_to root_path, notice: "Signed In Successfully"
elsif user_signed_in?
current_user.apply_omniauth(auth)
if current_user.save
current_user.update_attribute(:"allow_#{auth['provider']}_sync", true)
PullSocialActionsWorker.perform_async(current_user.id, auth["provider"])
redirect_to edit_social_profile_path, flash: { error: "#{auth["provider"]} Integration is processing" }
else
redirect_to edit_social_profile_path, flash: { error: "An error has occurred. Please try again." }
end
else
password = Devise.friendly_token[0,20]
athlete = Athlete.new(email: generate_auth_email(params[:provider]), password: password, password_confirmation: password )
athlete.apply_omniauth(auth)
begin
athlete.subscriptions.build(trial_expiry: DateTime.now + 30, active: true, account_type_id: AccountType.free.id)
if athlete.save(validate: false)
sign_in :user, athlete
redirect_to root_path, notice: "Account created and signed in successfully"
else
redirect_to root_path, flash: { error: "An error has occurred. Please try again." }
end
rescue ActiveRecord::RecordNotUnique
redirect_to root_path, flash: { error: "The email address you are trying to connect already exists. Perhaps you #{ActionController::Base.helpers.link_to "Forgot Your Password?", new_user_password_path}".html_safe }
end
end
end
def failure
redirect_to root_url, notice: "An Error has occurred. Please try again!"
end
private
def auth
JSON.parse($redis.get("omniauth_data"))
end
def generate_auth_email(provider)
if provider == "twitter"
"#{auth['uid']}@twitter.com"
else
auth['info']['email']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment