Skip to content
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

Http 502 config guard sanctum #117

Closed
sturn opened this issue Mar 27, 2020 · 5 comments
Closed

Http 502 config guard sanctum #117

sturn opened this issue Mar 27, 2020 · 5 comments

Comments

@sturn
Copy link

sturn commented Mar 27, 2020

  • Sanctum Version: 2.1
  • Laravel Version: 6.2
  • PHP Version: 7.3.10
  • Database Driver & Version:

Description:

Http 502

Steps To Reproduce:

  1. config\sanctum.php
    'guard' => 'sanctum',

  2. middleware
    Route::middleware('auth:sanctum')->get('/test', function (Request $request) { return []; });

I think

Compatibility Code

sanctum/src/Guard.php
demo

    /**
     * Retrieve the authenticated user for the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function __invoke(Request $request)
    {
        $statefulGuard = config('sanctum.guard', 'web');
        if (config("auth.guards.{$statefulGuard}.driver") != 'sanctum' && $user = $this->auth->guard($statefulGuard)->user()) {
            return $this->supportsTokens($user)
                        ? $user->withAccessToken(new TransientToken)
                        : $user;
        }
        ......
     }
@driesvints
Copy link
Member

Hey there,

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to report back and I'll gladly help you out and re-open this issue.

Thanks!

@AndreiTelteu
Copy link

AndreiTelteu commented Jan 23, 2022

I have this exact same issue. It took me a few hours to figure it out.

I fixed it by setting "guard" => null, in config/sanctum.php as this comment suggested: #149 (comment)

The problem was that i configured "guard" => ["sanctum"] thinking that this is how sanctum gets the logged in user.
This is causing an infinite loop i think: if ($user = $this->auth->guard($guard)->user()) { (Guard.php:57)

A simple improvement idea for sanctum is to check and skip the guard if it's using the sanctum guard (just before line 57).

if (config("auth.guards.$guard.driver") == "sanctum") {
    continue;
}

I'm glad to know that i'm not the only one making this mistake. @sturn and this stackoverflow user did the exact same mistake setting the sanctum.guard to the same sanctum guard.

@diego-lipinski-de-castro

just got into this problem today, if it gets too common I think its worth a note or some improvement into the framework

@olamideog
Copy link

Facing the same issue. Sanctum Guard is web, the guard on the route is auth:sanctum and the config/auth.php is using the default web guard.

@luanrodrigues
Copy link

same here problem here, solved setting it to null

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

No branches or pull requests

6 participants