使用设计测试rails API以进行注册.错误:"缺少'confirm_success_url'参数."

x6i*_*iae 10 ruby authentication ruby-on-rails devise ruby-on-rails-4

我试图用色器件devise_token_auth在我的应用程序进行身份验证.

我正在覆盖注册控制器,如下所示:

module Overrides
  class RegistrationsController < DeviseTokenAuth::RegistrationsController

    ## I am doing these to go around the `Can't verify CSRF token authenticity` Error.
    # skip_before_action :valid_authenticity_token, only: :create
    protect_from_forgery :except => :create


    def sign_up_params
      params.require(:user).permit(:email, :password, :password_confirmation, :name, :nickname)
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

我也使用swagger docs api发送我的参数如下:

swagger_api :create do
  summary "Sign up a new user"
  param :form, "user[email]", :string, :required, "Email of the new user"
  param :form, "user[password]", :string, :required, "Password of the new user"
  param :form, "user[password_confirmation]", :string, :required, "Retype Password of the new user"
  param :form, "user[name]", :string, :optional, "Name of the new user"
  param :form, "user[nickname]", :string, :optional, "Nick-Name of the new user"
  response :not_acceptable
  response :success
end
Run Code Online (Sandbox Code Playgroud)

这会在我的终端上生成如下参数:

Processing by Overrides::RegistrationsController#create as JSON
  Parameters: {"user"=>{"email"=>"sadf@fsd.fgs", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "name"=>"rgefsrfgrfdfsd", "nickname"=>"rgefsrfgrfdfsd"}}
Run Code Online (Sandbox Code Playgroud)

但是,这个请求永远不会影响我的模型,(无需创建新用户).但是,在页面上,我遇到以下错误:

错误响应的屏幕截图.

还有一个Completed 403 Forbidden in 93ms (Views: 0.3ms | ActiveRecord: 0.0ms)在终端上.

我怎样才能解决这个问题?谢谢

更新:

#route.rb: 
namespace :api do
    namespace :v1 do


      mount_devise_token_auth_for 'User', at: 'auth', controllers: {
                                          confirmations:      'overrides/confirmations',
                                          passwords:          'overrides/passwords',
                                          registrations:      'overrides/registrations',
                                          sessions:           'overrides/sessions',
                                        }
      ...

    end
  end


#Countroller:
class ApiController < ApplicationController
  include DeviseTokenAuth::Concerns::SetUserByToken
  protect_from_forgery with: :null_session
end

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  protect_from_forgery with: :null_session, if: Proc.new { |c| c.request.format == 'application/json' }
end

#user.rb:
class User < ActiveRecord::Base
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable,
          :confirmable, :omniauthable
  include DeviseTokenAuth::Concerns::User
Run Code Online (Sandbox Code Playgroud)

Imr*_*qvi 12

confirm_success_url参数用于支持多个客户端应用程序.如果不需要参数,则API无法知道在确认电子邮件后重定向的位置.

但是对于你devise_token_auth,你的application_controller.rb应该是:

  protect_from_forgery with: :null_session
  include DeviseTokenAuth::Concerns::SetUserByToken
Run Code Online (Sandbox Code Playgroud)

在您的routes.rb中

mount_devise_token_auth_for 'User', at: 'auth'

删除confirmable标签在user.rb中,使其如下所示

 devise :database_authenticatable, :recoverable,
         :trackable, :validatable, :registerable,
         :omniauthable

  include DeviseTokenAuth::Concerns::User
Run Code Online (Sandbox Code Playgroud)


Jim*_*eet 5

作为对可能发现此内容的任何人的说明,如果您有

  include DeviseTokenAuth::Concerns::User
Run Code Online (Sandbox Code Playgroud)

在 中devise部分之前user.rb,devise_token_auth 将为您包含一个模块列表,confirmable该列表包含在列表中(并且可能会产生上述错误)。只需在设计部分之后移动模块包含,它就会工作。

这不是 OP 遇到的问题,但这就是我到达这里的方式。


Sté*_*ert 5

解决方案是在中设置默认URL config/initializer/devise_token_auth.rb

DeviseTokenAuth.setup do |config|
    config.default_confirm_success_url = "confirmed"
end
Run Code Online (Sandbox Code Playgroud)