Skip to content

Instantly share code, notes, and snippets.

@kcurtin
Created July 31, 2012 14:02
Show Gist options
  • Save kcurtin/3217275 to your computer and use it in GitHub Desktop.
Save kcurtin/3217275 to your computer and use it in GitHub Desktop.
A much needed refactoring of the create action in my authentication controller, refactored code on top, original on bottom
class AuthenticationsController < ApplicationController
def create
omniauth = auth_hash
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
sign_in(authentication.user)
redirect_to root_url, notice: "Signed in!"
else
sign_in(User.create_from_omniauth(omniauth)) if current_user.blank?
current_user.create_authentication(omniauth)
if current_user.status == 'active'
redirect_to current_user, notice: "#{omniauth['provider']} has been added!"
elsif current_user.status == 'personal'
redirect_to '/user_steps/social', notice: "#{omniauth['provider']} has been added!"
else
redirect_to user_steps_path
end
end
end
...
end
class AuthenticationsController < ApplicationController
def create
omniauth = request.env['omniauth.auth']
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
sign_in(authentication.user)
redirect_to current_user, notice: "Signed in!"
elsif current_user
current_user.create_authentication(omniauth)
current_user.import_resume(omniauth)
if current_user.status == 'active'
redirect_to current_user, notice: "#{omniauth['provider']} has been added!"
else
redirect_to '/user_steps/social', notice: "#{omniauth['provider']} has been added!"
end
else
user = User.create_with_omniauth(omniauth)
if user.save
user.create_authentication(omniauth)
user.import_resume(omniauth)
sign_in(user)
redirect_to user_steps_path
else
redirect_to root_url, notice: "There was an error.."
end
end
end
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment