Skip to content

Automatically detect auth:sanctum #713

Answered by romalytvynenko
Shadercloud asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @Shadercloud

Yeah, there is no need to do it manually!

You can use operation transformers and detect the middleware there:

public function boot()
{
    Scramble::configure()
        ->withOperationTransformers(function (Operation $operation, RouteInfo $routeInfo) {
            $routeMiddleware = $routeInfo->route->gatherMiddleware();

            $hasAuthMiddleware = collect($routeMiddleware)->contains(
                fn ($m) => Str::startsWith($m, 'auth:')
            );
    
            if (! $hasAuthMiddleware) {
                $operation->addSecurity([]);
            }
        })
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Shadercloud
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants