在我的应用程序中,我有一个Twitter和facebook登录,但是我们需要在他们第一次注册twitter或facebook后提示密码和电子邮件。我正在使用omniauth gems,我的控制器/用户模型如下所示:
//socials_controller.rb
def create
#render text: request.env['omniauth.auth'].to_yaml and return
@user = User.from_omniauth(request.env['omniauth.auth'])
if(@user.email == nil)
redirect_to patient_login_entry_url(@user)
elsif @user.confirmed
log_in @user
redirect_to @user
else
flash[:danger] = "Bir hata olustu."
redirect_to root_url
end
end
def login_entry
@patient = Patient.find(params[:id])
end
def update_social
@patient = Patient.find(params[:id])
if @patient.update_attributes(user_params)
SendVerificationEmailJob.perform_later @patient
flash[:success] = "Aktivasyon için #{@patient.email} adresinizi kontrol ediniz."
redirect_to root_url
else
flash[:danger] = "Bilgilerinizi kontrol edip tekrar deneyiniz."
redirect_to patient_login_entry_url(@patient)
end
end
我的from_omniauth方法是:
//user.rb
has_secure_password
class << self
def from_omniauth(auth_hash)
if exists?(uid: auth_hash['uid'])
user = find_by(uid: auth_hash['uid'])
else
user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider'], type: 'Patient')
user.location = get_social_location_for user.provider, auth_hash['info']['location']
if auth_hash.provider == 'facebook'
user.avatar = User.process_uri(auth_hash['info']['image'])
user.name = auth_hash['extra']['raw_info']['first_name']
user.surname = auth_hash['extra']['raw_info']['last_name']
user.email = auth_hash['extra']['raw_info']['email']
user.gender = auth_hash['extra']['raw_info']['gender']
elsif auth_hash.provider == 'twitter'
user.avatar = auth_hash['info']['image']
user.name = auth_hash['info']['name']
end
user.url = get_social_url_for user.provider, auth_hash['info']['urls']
user.save!
end
user
end
在login_entry
页面,我只需提示电子邮件和密码,然后将其发布到update_social
方法。
但是,正如预期的那样,我的应用程序会抛出错误“密码不能为空”,因为默认情况下has_secure_password会验证其存在。因此,我需要在请求之间保留它,因为我无法在没有密码的情况下保存它。我怎样才能做到这一点?
我尝试使用to_json
方法将创建的对象存储在会话中,并将其转换为请求之间的哈希值,但这次我从twitter / facebook获取的个人资料图片没有保留(我正在使用AWS S3 + Paperclip,URL仍然存在,但是当我从S3控制台检查时没有这样的图像)所以我认为解决方案不好。