Rails基于Devise实现Google OAuth2登录回调报ArgumentError参数数量错误
问题描述
我参考公开教程为Rails应用配置用户认证能力,整套方案基于Devise和google_oauth2组件实现。
当用户尝试通过Google账号发起登录请求时,OAuth回调流程执行完成后会抛出ArgumentError异常。
已知排查进度
目前初步判断问题出在User模型与回调控制器的逻辑衔接环节,但受限于技术经验无法定位具体诱因,需要帮忙定位异常触发原因与对应的解决方案。
报错截图参考:
相关业务代码
User模型(user.rb)
class User < ApplicationRecord # 引入Devise默认功能模块,可选扩展模块包括: :confirmable, :lockable, :timeoutable, :trackable 和 :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable devise :omniauthable, omniauth_providers: [:google_oauth2] def self.from_google(email:, full_name:, uid:, avatar_url:) create_with(uid: uid, full_name: full_name, avatar_url: avatar_url).find_or_create_by!(email: email) end end
OAuth回调控制器(omniauth_callbacks_controller.rb)
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController def google_oauth2 user = User.from_google(from_google_params) if user.present? sign_out_all_scopes flash[:success] = t 'devise.omniauth_callbacks.success', kind: 'Google' sign_in_and_redirect user, event: :authentication else flash[:alert] = t 'devise.omniauth_callbacks.failure', kind: 'Google', reason: "#{auth.info.email} is not authorized." redirect_to new_user_session_path end end protected def after_omniauth_failure_path_for(_scope) new_user_session_path end def after_sign_in_path_for(resource_or_scope) stored_location_for(resource_or_scope) || root_path end private def from_google_params @from_google_params ||= { uid: auth.uid, email: auth.info.email, full_name: auth.info.name, avatar_url: auth.info.image } end def auth @auth ||= request.env['omniauth.auth'] end end
内容的提问来源于stack exchange,提问作者rcrusoe
相关产品推荐
相关产品推荐


