ruby-on-rails – 使用omniauth时设计跳过确认

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 使用omniauth时设计跳过确认前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何更改以下代码,以便登录Facebook的用户可以通过设计确认跳过确认?我尝试添加user.skip_confirmation!在user.email行下…但是这不起作用.

user.rb:

  1. # Finds or creates user based on omniauth hash from given provider
  2. def self.from_omniauth(auth)
  3. where(provider: auth.provider,uid: auth.uid).first_or_create do |user|
  4. user.provider = auth.provider
  5. user.uid = auth.uid
  6. user.first_name = auth.info.first_name
  7. user.last_name = auth.info.last_name
  8. user.email = auth.info.email
  9. end
  10. end
  11.  
  12. # Overrides class method 'new_with_session' to persist and validate attributes
  13. def self.new_with_session(params,session)
  14. if session["devise.user_attributes"]
  15. new(session["devise.user_attributes"],without_protection: true) do |user|
  16. user.attributes = params
  17. user.valid?
  18. end
  19. else
  20. super
  21. end
  22. end

omn​​iauth_callbacks_controller.rb

  1. class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  2.  
  3. def all
  4. user = User.from_omniauth(request.env["omniauth.auth"])
  5. if user.persisted?
  6. flash.notice = "Signed in!"
  7. sign_in_and_redirect user
  8. else
  9. session["devise.user_attributes"] = user.attributes
  10. redirect_to new_user_registration_url
  11. end
  12. end
  13. alias_method :facebook,:all
  14. end

解决方法

尝试使用first_or_initialize:
  1. def self.from_omniauth(auth)
  2. where(provider: auth.provider,uid: auth.uid). first_or_initialize do |user|
  3. user.provider = auth.provider
  4. user.uid = auth.uid
  5. user.first_name = auth.info.first_name
  6. user.last_name = auth.info.last_name
  7. user.email = auth.info.email
  8. user.skip_confirmation!
  9. user.save!
  10. end
  11. end

猜你在找的Ruby相关文章