-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assertCredentials
is throwing an error
#268
Comments
Since Sanctum works token based, this method is incompatible with Sanctum. |
Sanctum is not only token-based, it relies on the user provider of the default guard for authenticating/fetching users for SPA. You can even specify a user provider in Even on token-based authentication, it checks the provider (if specified): https://github.com/laravel/sanctum/blob/2.x/src/Guard.php#L71 |
Heya, thanks for reporting. I'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up. laravel new sanctum-issue-268 --github="--public" After you've posted the repository, I'll try to reproduce the issue. Thanks! |
Hey @driesvints, I've created a repo for reproducing this issue: https://github.com/sebdesign/sanctum-issue-268 After the installation, I've installed Sanctum without any customization, in a separate commit. I'm using an in-memory SQLite database, so after installing this you can run I hope that's not confusing and that you can understand the process. The key here is that the sanctum guard is missing something to make it work well with the underlying user provider. |
I have to postpone looking into this a bit. I tried setting up the project locally but it causes PhpStorm for me to freeze and crash all the time. It's specific to this project and my colleagues aren't experiencing the same thing so it's specific to my device. I've contacted JetBrains in the hope to figure that out. Please do not remove your test repo for the time being so they can debug themselves. In the meantime: I noticed you're sending multiple HTTP calls in a single test. This isn't officially support and can lead to edge cases with state management. You're also using an in-memory sqlite database which can be a culprit in combination with this. Please try the following to see if that helps in resolving to the problem:
Please do not commit these changes to the test repo so JetBrains can use it in its current state to debug the crashing issue. |
Thanks for the explanation! I'm using Visual Studio code with Ubuntu 18.04 in WSL2, so I don't know what might cause this. The only reason I'm doing all these HTTP calls is for sanity checks, as a way to describe a more realistic flow. I don't believe this is a database issue, because the reason the user provider is |
I think I understand what's going on. It's not so much the sanctum guard that you need to target but the $this->assertCredentials([
'email' => '[email protected]',
'password' => 'password',
], 'web'); Your tests will pass. Also see notes like this in the docs: |
Description:
I'm using Sanctum for SPA authentication on an existing project, and while testing a new feature, I noticed that
$this->assertCredentials()
throws an error when accessing a route with theauth:sanctum
middleware.The reason of this error is that the default guard is
sanctum
, which doesn't have a provider by default.I've tried calling
$this->actingAs($user, 'sanctum')
and$this->assertCredentials([..], 'sanctum')
and that still fails.The only way that works is
$this->assertCredentials([...], 'web')
.Another way to fix this is to add this to
config/auth.php
:I remember fixing some related issue in #225 , but that's still not enough.
In this comment #225 (comment) I mentioned that the root of the problem is #149, which is not implemented correctly. That implementation works when configuring custom guards, but not with a vanilla Sanctum installation.
Steps To Reproduce:
The text was updated successfully, but these errors were encountered: