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

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

Constant Summary

Constants included from ChannelResolution

ChannelResolution::CHANNEL_HEADER

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": "..." }


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

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 submitted refresh token. The token itself is the credential — no access JWT is required, so clients with an expired access token can still log out.



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

def logout
  refresh_token_value = params[:refresh_token]

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

  head :no_content
end

#refreshObject

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



38
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
# File 'app/controllers/spree/api/v3/store/auth_controller.rb', line 38

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