Rails Omniauth-github(422)您想要的更改被拒绝



我有这个Omniauth解决方案&Github实现并工作良好,但在过去几个月的某个时候它停止工作了。

当我尝试登录时得到的错误是:(422)您想要的更改被拒绝。

特别是在我看到的Heroku日志中:

ActiveRecord::RecordInvalid (Validation failed: Password can't be blank):
app/models/user.rb:18:in `create_from_omniauth'
app/models/user.rb:14:in `from_omniauth'
app/controllers/sessions_controller.rb:4:in `create'

创建用户时需要保存凭据吗?

我的用户模型:

def self.from_omniauth(auth)
  where(auth.slice("provider", "uid")).first || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
  create! do |user|
    user.provider = auth["provider"]
    user.uid = auth["uid"]
    user.name = auth["info"]["nickname"]
    user.email = auth["info"]["email"]
    user.image = auth["info"]["image"]
 end
end

会话控制器:

class SessionsController < ApplicationController
def create
  user = User.from_omniauth(env["omniauth.auth"])
  session[:user_id] = user.id
  redirect_to root_url, notice: "Signed in!"
end
def destroy
  session[:user_id] = nil
  redirect_to root_url, notice: "Signed out!"
 end
end

Facebook的全能错误"您想要的更改被拒绝"可能会出现,因为您在模型中设置了验证。我必须为拥有唯一电子邮件的用户重构我的验证,当用户试图用相同的电子邮件登录facebook时,这就不起作用了。

看你的日志。heroku logs -t

看起来您要么在验证User模型中密码字段的存在,要么使用has_secure_password,它在掩护下完成。

如果你自己做验证,你可以只添加一个子句,如:if => :password_changed?到验证。

如果你使用has_secure_password,这取决于你使用的是哪个版本的Rails。任何有这两个变化的版本(我相信只有Rails 4)都支持将validations: false选项传递给has_secure_password。否则,除了在创建用户时设置一个随机的虚拟密码,然后让他们立即更改密码之外,就没有真正好的解决方案了。

当我的gitlab服务器上的时间不同步时,我遇到了这个问题,我重新启动了ntpd,它纠正了服务器上的时间,问题解决了