Created
October 20, 2016 21:24
-
-
Save seguelador/4fd6802340e2501b4f99ade73de0b366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
devise.rb | |
config.omniauth :google_oauth2, Rails.application.secrets.google_oauth2_key, Rails.application.secrets.google_oauth2_secret, | |
scope: 'email, profile', image_aspect_ratio: 'square', image_size: 300, access_type: 'online' | |
user.rb | |
def self.find_for_oauth(auth, signed_in_resource = nil) | |
identity = Identity.find_for_oauth(auth) | |
user = signed_in_resource ? signed_in_resource : identity.user | |
if user.nil? | |
email = auth.info.email | |
user = User.find_by(email: email) if email | |
# Create the user if it's a new registration | |
if user.nil? | |
password = Devise.friendly_token[0,20] | |
name = auth.info.name | |
profile_picture = auth.info.image | |
if auth.provider == 'facebook' | |
user = User.new( | |
email: email ? email : "#{auth.uid}@change-me.com", | |
name: name, | |
remote_avatar_url: profile_picture, | |
password: password, | |
password_confirmation: password | |
) | |
elsif auth.provider == 'twitter' | |
user = User.new( | |
email: email ? email : "#{auth.uid}@change-me.com", | |
name: name, | |
remote_avatar_url: profile_picture, | |
password: password, | |
password_confirmation: password | |
) | |
elsif auth.provider == 'google_oauth2' | |
user = User.new( | |
email: email ? email : "#{auth.uid}@change-me.com", | |
name: name, | |
remote_avatar_url: profile_picture, | |
password: password, | |
password_confirmation: password | |
) | |
end | |
end | |
#user.skip_confirmation! | |
#user.save! <-- this line is generating a conflict with usual (email-way) sign up | |
end | |
if identity.user != user | |
identity.user = user | |
identity.save! | |
end | |
return user | |
end | |
omniauth_callback_controller | |
class 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(:success, :success, kind: provider_name("#{provider}")) if is_navigational_format? | |
else | |
session["devise.#{provider}_data"] = env["omniauth.auth"] | |
redirect_to new_user_registration_url | |
end | |
end | |
} | |
end | |
[:twitter, :facebook, :google_oauth2].each do |provider| | |
provides_callback_for provider | |
end | |
def after_sign_in_path_for(resource) # Revisa después de cada login si el mail del usuario es válido | |
if resource.email_verified? | |
super resource # Acción por defecto de Devise (si no está configurada, va al root_path) | |
else | |
finish_signup_path(resource) | |
end | |
end | |
def provider_name(provider) | |
{twitter: 'Twitter', facebook: 'Facebook', google_oauth2: 'Google'}[provider.to_sym] || provider.capitalize | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment