Refactor first_or_create_for_auth & its usage

Conflicts:
	app/controllers/users/omniauth_callbacks_controller.rb

Refactors first_or_initialize_for_oauth
This commit is contained in:
kikito
2016-01-13 12:31:07 +01:00
parent f262686023
commit d1741a2b28
2 changed files with 38 additions and 46 deletions

View File

@@ -1,23 +1,15 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def self.provides_callback_for(provider)
class_eval %Q{
def #{provider}
@user = User.find_for_oauth(env["omniauth.auth"], current_user)
if @user.persisted?
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
else
session["devise.#{provider}_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
}
def twitter
sign_in_with :twitter
end
[:twitter, :facebook, :google_oauth2].each do |provider|
provides_callback_for provider
def facebook
sign_in_with :facebook
end
def google_oauth2
sign_in_with :google_oauth2
end
def after_sign_in_path_for(resource)
@@ -28,4 +20,22 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
end
end
private
def sign_in_with(provider)
auth = env["omniauth.auth"]
identity = Identity.first_or_create_from_oauth(auth)
@user = current_user || identity.user || User.first_or_initialize_for_oauth(auth)
if @user.save
identity.update(user: @user)
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: "#{provider}".capitalize) if is_navigational_format?
else
session["devise.#{provider}_data"] = env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end