Automatically detect auth:sanctum #713
Answered
by
romalytvynenko
Shadercloud
asked this question in
Q&A
-
Is there a way to configure scramble to automatically mark any routes behind the auth:sanctum middleware as requiring authentication; and have all other routes be unauthenticated? Reading the docs it seems like I would need to go through and manually add the tag: Above all controller methods that do not require auth...it would be a lot easier to have it auto detect this from the middleware. |
Beta Was this translation helpful? Give feedback.
Answered by
romalytvynenko
Feb 10, 2025
Replies: 1 comment
-
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([]);
}
})
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Shadercloud
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @Shadercloud
Yeah, there is no need to do it manually!
You can use operation transformers and detect the middleware there: