Class: Spree::Api::V3::Store::CustomersController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/api/v3/store/customers_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::RATE_LIMIT_RESPONSE

Constants included from Idempotent

Idempotent::IDEMPOTENCY_HEADER, Idempotent::IDEMPOTENCY_TTL, Idempotent::MAX_KEY_LENGTH, Idempotent::MUTATING_METHODS

Constants included from ErrorHandler

ErrorHandler::ERROR_CODES

Constants included from JwtAuthentication

JwtAuthentication::JWT_AUDIENCE_ADMIN, JwtAuthentication::JWT_AUDIENCE_STORE, JwtAuthentication::JWT_ISSUER, JwtAuthentication::USER_TYPE_ADMIN, JwtAuthentication::USER_TYPE_CUSTOMER

Instance Method Summary collapse

Methods included from ApiKeyAuthentication

#authenticate_api_key!, #authenticate_secret_key!

Methods included from JwtAuthentication

#authenticate_user, #require_authentication!

Instance Method Details

#createObject

POST /api/v3/store/customers



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/spree/api/v3/store/customers_controller.rb', line 12

def create
  user = Spree.user_class.new(permitted_params.except(:current_password))

  if user.save
    refresh_token = Spree::RefreshToken.create_for(user, request_env: {
      ip_address: request.remote_ip,
      user_agent: request.user_agent&.truncate(255)
    })
    render json: {
      token: generate_jwt(user),
      refresh_token: refresh_token.token,
      user: user_serializer.new(user, params: serializer_params).to_h
    }, status: :created
  else
    render_errors(user.errors)
  end
end

#showObject

GET /api/v3/store/customer



31
32
33
# File 'app/controllers/spree/api/v3/store/customers_controller.rb', line 31

def show
  render json: serialize_resource(current_user)
end

#updateObject

PATCH /api/v3/store/customer



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/spree/api/v3/store/customers_controller.rb', line 36

def update
  if sensitive_update? && !valid_current_password?
    return render_error(
      code: ErrorHandler::ERROR_CODES[:current_password_invalid],
      message: Spree.t(:current_password_invalid, scope: :api),
      status: :unprocessable_content
    )
  end

  update_params = permitted_params.except(:current_password)

  if current_user.update(update_params)
    render json: serialize_resource(current_user)
  else
    render_errors(current_user.errors)
  end
end