Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storefront account create #489

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@
get '/logout' => 'user_sessions#destroy', :as => :logout
end
end

namespace :api, defaults: { format: 'json' } do
namespace :v2 do
namespace :storefront do
resource :account, controller: :account, only: %i[show create]
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Spree
module Api
module V2
module Storefront
module AccountControllerDecorator
def self.prepended(base)
base.skip_before_action :require_spree_current_user, only: [:create]
end

def create
user = Spree.user_class.new(spree_user_params)
if user.save
render_serialized_payload { serialize_resource(user) }
else
render_error_payload(structed_error(user.errors))
end
end

def spree_user_params
params.require(:user).permit(Spree::PermittedAttributes.user_attributes)
end

def structed_error(error)
struct_error = Struct.new(:error) do
def to_s
return error.full_messages.join(', ') if error&.respond_to?(:full_messages)

value.to_s
end

def to_h
return error.messages if error&.respond_to?(:messages)

{}
end
end

struct_error.new(error)
end
end
end
end
end
end

::Spree::Api::V2::Storefront::AccountController.prepend(Spree::Api::V2::Storefront::AccountControllerDecorator)
2 changes: 1 addition & 1 deletion lib/spree/auth/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def self.activate
'lib/assets/javascripts/spree/frontend/spree_auth.js',
'lib/assets/javascripts/spree/frontend/spree_auth.css'
]
Dir.glob(File.join(File.dirname(__FILE__), "../../controllers/frontend/*/*_decorator*.rb")) do |c|
Dir.glob(File.join(File.dirname(__FILE__), "../../controllers/frontend/**/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
Expand Down
42 changes: 42 additions & 0 deletions spec/requests/spree/api/v2/storefront/account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

describe 'Storefront API v2 Account spec', type: :request do
describe 'account#create' do
before { post '/api/v2/storefront/account', params: params }

context 'valid user params' do
let(:params) do
{
"user": {
"email": "[email protected]",
"password": "password123",
"password_confirmation": "password123"
}
}
end

it_behaves_like 'returns 200 HTTP status'

it 'return JSON API payload of User' do
expect(JSON.parse(response.body)['data']['attributes']['email']).to eq('[email protected]')
end
end

context 'invalid user params' do
let(:params) do
{
"user": {
"email": "[email protected]",
"password": "password123",
"password_confirmation": ""
}
}
end

it 'return JSON API payload of error' do
expect(JSON.parse(response.body)['error']).to eq("Password Confirmation doesn't match Password")
end
end

end
end