Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. devise.rb
  2.  
  3. config.omniauth :google_oauth2, Rails.application.secrets.google_oauth2_key, Rails.application.secrets.google_oauth2_secret,
  4. scope: 'email, profile', image_aspect_ratio: 'square', image_size: 300, access_type: 'online'
  5.  
  6. user.rb
  7.  
  8. def self.find_for_oauth(auth, signed_in_resource = nil)
  9. identity = Identity.find_for_oauth(auth)
  10. user = signed_in_resource ? signed_in_resource : identity.user
  11.  
  12. if user.nil?
  13. email = auth.info.email
  14. user = User.find_by(email: email) if email
  15.  
  16. # Create the user if it's a new registration
  17. if user.nil?
  18. password = Devise.friendly_token[0,20]
  19. name = auth.info.name
  20. profile_picture = auth.info.image
  21. if auth.provider == 'facebook'
  22. user = User.new(
  23. email: email ? email : "#{auth.uid}@change-me.com",
  24. name: name,
  25. remote_avatar_url: profile_picture,
  26. password: password,
  27. password_confirmation: password
  28. )
  29. elsif auth.provider == 'twitter'
  30. user = User.new(
  31. email: email ? email : "#{auth.uid}@change-me.com",
  32. name: name,
  33. remote_avatar_url: profile_picture,
  34. password: password,
  35. password_confirmation: password
  36. )
  37. elsif auth.provider == 'google_oauth2'
  38. user = User.new(
  39. email: email ? email : "#{auth.uid}@change-me.com",
  40. name: name,
  41. remote_avatar_url: profile_picture,
  42. password: password,
  43. password_confirmation: password
  44. )
  45. end
  46. end
  47. #user.skip_confirmation!
  48. #user.save! <-- this line is generating a conflict with usual (email-way) sign up
  49. end
  50.  
  51. if identity.user != user
  52. identity.user = user
  53. identity.save!
  54. end
  55.  
  56. return user
  57. end
  58.  
  59.  
  60. omniauth_callback_controller
  61.  
  62. class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  63. def self.provides_callback_for(provider)
  64. class_eval %Q{
  65. def #{provider}
  66. @user = User.find_for_oauth(env["omniauth.auth"], current_user)
  67.  
  68. if @user.persisted?
  69. sign_in_and_redirect @user, event: :authentication
  70. set_flash_message(:success, :success, kind: provider_name("#{provider}")) if is_navigational_format?
  71. else
  72. session["devise.#{provider}_data"] = env["omniauth.auth"]
  73. redirect_to new_user_registration_url
  74. end
  75. end
  76. }
  77. end
  78.  
  79. [:twitter, :facebook, :google_oauth2].each do |provider|
  80. provides_callback_for provider
  81. end
  82.  
  83. def after_sign_in_path_for(resource) # Revisa después de cada login si el mail del usuario es válido
  84. if resource.email_verified?
  85. super resource # Acción por defecto de Devise (si no está configurada, va al root_path)
  86. else
  87. finish_signup_path(resource)
  88. end
  89. end
  90.  
  91. def provider_name(provider)
  92. {twitter: 'Twitter', facebook: 'Facebook', google_oauth2: 'Google'}[provider.to_sym] || provider.capitalize
  93. end
  94.  
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement