设计,Omniauth和'新会议'

Mat*_*zzi 3 authentication ruby-on-rails devise omniauth

我在我的用户模型(使用设计和确认)中有一个方法,new_with_session根据Omniauth + Devise(https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview)的要求调用:

def self.new_with_session(params, session)
  super.tap do |user|
    if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] || session["devise.google_data"] && session["devise.google_data"]["extra"]["raw_info"]
      user.email = data["email"]
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

允许用户使用Google或Facebook登录,我使用此行保存权限user.email:

if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] || session["devise.google_data"] && session["devise.google_data"]["extra"]["raw_info"]
Run Code Online (Sandbox Code Playgroud)

但我不认为是正确的方式,所以......

  1. 您是否知道user.email比使用||运营商更好的构建方式?
  2. 如果我想从Google/Facebook中保存更多数据,比如用户名,我应该将其添加到我的自定义中new_with_session吗?如果是这样,为什么?

小智 9

new_with_session用于build_resource.这与可注册(用户注册表单)一起使用.

这仅在您的Facebook/Omniauth会话已存在并且您希望使用omniauth的某些数据预填充注册表单时才有用.(假设您尚未在回调时自动创建帐户)

# Build a devise resource passing in the session. Useful to move
# temporary session data to the newly created user.
def build_resource(hash=nil)
  hash ||= params[resource_name] || {}
  self.resource = resource_class.new_with_session(hash, session)
end
Run Code Online (Sandbox Code Playgroud)