gpt4 book ai didi

ruby-on-rails - self.new_with_session(params, session) 在这种情况下做什么

转载 作者:行者123 更新时间:2023-12-04 03:12:01 26 4
gpt4 key购买 nike

我正在尝试在 Rails 应用程序中实现 facebook 登录,我使用了以下示例中的代码:https://github.com/rails-camp/facebook-omniauth-demo

# app/models/user.rb

def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end

def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end

self.new_with_session(params, session) 方法在这种情况下具体做什么?

最佳答案

你正在覆盖 new_with_session 函数。

def self.new_with_session(params, session)
# call super(the overwritten function), it will return a resource and in this case you are
# naming it 'user' in the block
super.tap do |user|
# if extra information was provided by facebook when user logged in, assign whatever comes in
# session["devise.facebook_data"]["extra"]["raw_info"] to 'data' variable
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
# assign 'email' given by facebook in case the 'user' object doesn't have one yet
user.email = data["email"] if user.email.blank?
end
end
end

关于ruby-on-rails - self.new_with_session(params, session) 在这种情况下做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44446166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com