Spree API身份验证扩展无效

时间:2018-11-05 16:55:57

标签: ruby-on-rails ruby spree

我正在开发一个扩展,以通过Spree API对用户进行身份验证。我进行了很多搜索,但几乎没有关于该主题的信息,而且我发现的一些扩展程序无效。

我根据找到的扩展名创建了扩展名,但出现以下错误:

AbstractController :: ActionNotFound(对于Spree :: UsersController找不到操作'sign_in')

module Spree
  module Api

      #class UsersController < Spree::Api::V1::UsersController 
      Spree::Api::V1::UsersController.class_eval do
        before_action :authenticate_user, :except => [:sign_up, :sign_in]

        def sign_up

          @user = Spree::User.find_by_email(params[:user][:email])

          if @user.present?
            render "spree/api/users/user_exists", :status => 401 and return
          end

          @user = Spree::User.new(user_params)
          if !@user.save
            unauthorized
            return
          end
          @user.generate_spree_api_key!
        end

        def sign_in
          puts("hola controller")
          @user = Spree::User.find_by_email(params[:user][:email])
          if !@user.present? || !@user.valid_password?(params[:user][:password])
            unauthorized
            return
          end
          @user.generate_spree_api_key! if @user.spree_api_key.blank?
        end


        def user_params
          params.require(:user).permit(:email, :password, :password_confirmation)
        end


    #end
    end
  end
end

我的路线。rb

Spree::Core::Engine.add_routes do
  # Add your extension routes here
  namespace :api, defaults: {format: 'json'} do
    resource :users do
        #resource :v1 do
        member do
          #post :sign_up
          post :sign_in
        end
    #end
    end
  end
end

您能帮我弄清楚我在想什么吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您需要在config/routes.rb的v1名称空间中添加路由:

namespace :api do
  namespace :v1 do
    resource :users do
      member do
        post :sign_in
      end
    end
  end
end