Skip to content

Conversation

@hasbi-ashshidiq23
Copy link
Contributor

@hasbi-ashshidiq23 hasbi-ashshidiq23 commented Dec 2, 2025

Background

Users reported being unable to redeem rewards using points, receiving this error:

"Gagal membuka order. Mohon untuk menghubungi Tim Development untuk segera mengatasi masalah ini."

After tracing the execution flow inside OrderStoreAction, the issue originates from this invariant check:

// Dicoding/Domain/Order/Contracts/Order.php:127
public function setOrderer(Member $orderer)
{
    if ($this->getOrdererId() !== $orderer->getId()) {
        throw new InvariantException('ORDER.INVALID_ORDERER');
    }

    $this->orderer = $orderer;
}

Why does this comparison fail?

getOrdererId() and $orderer->getId() should represent the same authenticated user ID — but:

  • getOrdererId()string "283"
  • $orderer->getId()integer 283

Because the comparison uses strict (!==), the mismatch throws ORDER.INVALID_ORDERER.


Root Cause

getOrdererId() gets its value from Auth::id().
And Auth::id() pulls the ID from Illuminate/Auth/Guard::id() in two ways:

$id = $this->session->get($this->getName(), $this->getRecallerId());

Meaning:

  • If the session is active, $id comes from the session
  • If the session is expired, it falls back to getRecallerId()

getRecallerId() extracts the user ID from the “remember me” cookie:

// remember me token example: "283|token"
return head(explode('|', $recaller));

This function returns the head as a string, so Auth::id() becomes a string whenever the user is authenticated via the "remember me" cookie.

This creates the strict-comparison mismatch in Order::setOrderer().

Changes Introduced

  • Cast getRecallerId() return value to int
    Ensures consistent data type for user IDs returned by Auth::id(), regardless of session state.
  • Added a test verifying that when session value is missing and authentication falls back to the remember-me recaller Auth::id() always returns an integer

This prevents regressions and ensures type consistency across the authentication layer.

Impact

  • Fixes reward redemption failures caused by the mismatched strict comparison.
  • Ensures consistent integer IDs from Laravel’s authentication guard.
  • Prevents future silent failures in domain logic that rely on strict ID checks.

Detailed documentation: https://www.notion.so/dicoding/Root-Cause-Analysis-Auth-id-Returning-string-2bd92c6a7b91804cb18dc435f77fdc58

@hasbi-ashshidiq23 hasbi-ashshidiq23 self-assigned this Dec 2, 2025
@hasbi-ashshidiq23 hasbi-ashshidiq23 force-pushed the bugfix/cast-id-from-remember-token-to-int branch from 6fff4c4 to 0601ca8 Compare December 2, 2025 08:20
@hasbi-ashshidiq23 hasbi-ashshidiq23 marked this pull request as ready for review December 2, 2025 09:04
also add test for returning integer ID from remember cookie
@hasbi-ashshidiq23 hasbi-ashshidiq23 force-pushed the bugfix/cast-id-from-remember-token-to-int branch from 0601ca8 to f37d7da Compare December 2, 2025 09:05
Copy link
Member

@rizqyhi rizqyhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good ya @hasbi-ashshidiq23 👍

@rizqyhi rizqyhi merged commit 2286fc8 into master Dec 3, 2025
1 check passed
@rizqyhi rizqyhi deleted the bugfix/cast-id-from-remember-token-to-int branch December 3, 2025 14:02
@hasbi-ashshidiq23
Copy link
Contributor Author

mantap mang @rizqyhi terimakasih bantuannya

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants