Skip to content

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

Open
prateekbhujel wants to merge 1 commit into
NativePHP:mainfrom
prateekbhujel:feat/user-has-api-tokens
Open

prateekbhujel wants to merge 1 commit into
NativePHP:mainfrom
prateekbhujel:feat/user-has-api-tokens

Conversation

@prateekbhujel

Copy link
Copy Markdown

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.
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.

1 participant