Skip to content

Add HasApiTokens trait to User model for Sanctum token authentication - #2

Merged
shanerbaner82 merged 1 commit into
NativePHP:mainfrom
prateekbhujel:feat/user-has-api-tokens
Sep 20, 2026
Merged

shanerbaner82 merged 1 commit into
NativePHP:mainfrom
prateekbhujel:feat/user-has-api-tokens

Conversation

@prateekbhujel

Copy link
Copy Markdown
Contributor

The documentation describes Sanctum as wired for API token authentication:

  • README.md:

    "API auth | laravel/sanctum — wired, not demoed"
    "- API: Sanctum is installed (routes/api.php) — wired for token auth, not demoed with sample endpoints beyond /api/user"

  • CONTRIBUTING.md:

    "Sanctum API | routes/api.php, config/sanctum.php | No (wired, not demoed)"

However, App\Models\User was missing Laravel\Sanctum\HasApiTokens, so calling $user->createToken(...) throws:

BadMethodCallException: Call to undefined method App\Models\User::createToken()

Changes

  • Added HasApiTokens trait to App\Models\User.
  • Added Pest feature tests in tests/Feature/ApiAuthTest.php covering token creation, unauthenticated access rejection, and authenticated requests with Sanctum tokens.

Allow User instances to issue Sanctum personal access tokens via
createToken() as documented in README and CONTRIBUTING. Also add Pest
feature tests verifying token issuance and /api/user authentication.
@shanerbaner82
shanerbaner82 merged commit 32a65d9 into NativePHP:main Sep 20, 2026
@prateekbhujel
prateekbhujel deleted the feat/user-has-api-tokens branch September 21, 2026 19:36
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.

2 participants