Class: Spree::Api::V3::Store::AuthController

Inherits:
BaseController show all
Defined in:
app/controllers/spree/api/v3/store/auth_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/auth/login Supports multiple authentication providers via :provider param Example:

{ "provider": "email", "email": "...", "password": "..." }


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/spree/api/v3/store/auth_controller.rb', line 18

def create
  strategy = authentication_strategy
  return unless strategy # Error already rendered by determine_strategy

  result = strategy.authenticate

  if result.success?
    user = result.value
    render json: auth_response(user)
  else
    render_error(
      code: ERROR_CODES[:authentication_failed],
      message: result.error,
      status: :unauthorized
    )
  end
end

#logoutObject

POST /api/v3/store/auth/logout Accepts: { “refresh_token”: “rt_xxx” } Revokes the refresh token



73
74
75
76
77
78
79
80
81
# File 'app/controllers/spree/api/v3/store/auth_controller.rb', line 73

def logout
  refresh_token_value = params[:refresh_token]

  if refresh_token_value.present?
    Spree::RefreshToken.find_by(token: refresh_token_value)&.destroy
  end

  head :no_content
end

#oauth_callbackObject

POST /api/v3/store/auth/oauth/callback OAuth callback endpoint for server-side OAuth flows



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/spree/api/v3/store/auth_controller.rb', line 85

def oauth_callback
  strategy = authentication_strategy
  return unless strategy # Error already rendered by determine_strategy

  result = strategy.authenticate

  if result.success?
    user = result.value
    render json: auth_response(user)
  else
    render_error(
      code: ERROR_CODES[:authentication_failed],
      message: result.error,
      status: :unauthorized
    )
  end
end

#refreshObject

POST /api/v3/store/auth/refresh Accepts: { “refresh_token”: “rt_xxx” } Returns new access JWT + rotated refresh token



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/spree/api/v3/store/auth_controller.rb', line 39

def refresh
  refresh_token_value = params[:refresh_token]

  if refresh_token_value.blank?
    return render_error(
      code: ERROR_CODES[:invalid_refresh_token],
      message: 'refresh_token is required',
      status: :unauthorized
    )
  end

  refresh_token = Spree::RefreshToken.active.find_by(token: refresh_token_value)

  if refresh_token.nil?
    return render_error(
      code: ERROR_CODES[:invalid_refresh_token],
      message: 'Invalid or expired refresh token',
      status: :unauthorized
    )
  end

  user = refresh_token.user
  new_refresh_token = refresh_token.rotate!(request_env: request_env_for_token)

  render json: {
    token: generate_jwt(user),
    refresh_token: new_refresh_token.token,
    user: user_serializer.new(user, params: serializer_params).to_h
  }
end