我为google_oauth2定义了一个控制器方法,但是仍然缺少

问题描述 投票:0回答:2

[当我尝试进行身份验证时,收到消息:

"Not found. Authentication passthru."

我添加了一个action_missing方法来获得线索:调用它时,它会记录日志:

  Parameters: {"provider"=>"google_oauth2"}

因此,似乎有人抱怨我缺少所定义的方法。为什么我的动作在定义后会丢失?

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def google_oauth2
    @user = User.from_omniauth(request.env["omniauth.auth"])

    if @user.persisted?
      flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google"
      sign_in_and_redirect @user, :event => :authentication
    else
      session["devise.google_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

  def action_missing(provider)
    logger.debug provider
  end

end

devise.rb:

config.omniauth :google_oauth2,
  'my',
  'secret',
  {
      :name => "google",
      :scope => 'email, profile',
      :prompt => 'select_account',
      :image_aspect_ratio => 'square',
      :image_size => 50
  }

User.rb:

class User < ActiveRecord::Base
  devise :omniauthable, :omniauth_providers => [:google_oauth2]
  def self.from_omniauth(access_token)
    data = access_token.info
    user = User.where(:email => data["email"]).first

    # Uncomment the section below if you want users to be created if they don't exist
    # unless user
    #     user = User.create(name: data["name"],
    #        email: data["email"],
    #        password: Devise.friendly_token[0,20]
    #     )
    # end
    user
  end
end
ruby-on-rails devise
2个回答
1
投票

如果您为配置设置提供:name,则必须使用给定名称进行调用。您在此处指定的授权重定向URI的名称也将为“ google”。


1
投票

我发现了我的错误,其他人可能会发现它很有启发性:

在devise.rb中,我有:

 {
      :name => 'google', <====== BAD IDEA!
      :scope => 'email, profile',
      :prompt => 'select_account',
      :image_aspect_ratio => 'square',
      :image_size => 50
  }

第一项:name是从另一个omniauth教程复制而来的。这似乎是一个非常糟糕的主意。删除它可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.