繁体   English   中英

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

[英]Testing rails API for sign-up with devise. Error: “Missing 'confirm_success_url' parameter.”

我试图用色器件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

我也使用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

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

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

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

错误响应的屏幕截图。

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

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

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

  protect_from_forgery with: :null_session
  include DeviseTokenAuth::Concerns::SetUserByToken

在您的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

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

DeviseTokenAuth.setup do |config|
    config.default_confirm_success_url = "confirmed"
end

作为对任何可能发现这一点的人的记录,如果你有

  include DeviseTokenAuth::Concerns::User

之前 devise在部分user.rb ,devise_token_auth将包括模块列表给你,其中包括confirmable列表中(并可能产生上面的错误)。 只需在设计部分之后移动模块包含它就可以了。

这不是OP的问题,而是我到达这里的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM