generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unauthenticated response documentation (#392)
* `Improvement` | Add 401 Unauthorized response to routes with `auth:sanctum` middleware or `Illuminate\Auth\AuthenticationException` exception * added tests and proper auth error message --------- Co-authored-by: Roman Lytvynenko <[email protected]>
- Loading branch information
1 parent
a13aeed
commit ab22585
Showing
5 changed files
with
96 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Support/ExceptionToResponseExtensions/AuthenticationExceptionToResponseExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\ExceptionToResponseExtensions; | ||
|
||
use Dedoc\Scramble\Extensions\ExceptionToResponseExtension; | ||
use Dedoc\Scramble\Support\Generator\Reference; | ||
use Dedoc\Scramble\Support\Generator\Response; | ||
use Dedoc\Scramble\Support\Generator\Schema; | ||
use Dedoc\Scramble\Support\Generator\Types as OpenApiTypes; | ||
use Dedoc\Scramble\Support\Type\ObjectType; | ||
use Dedoc\Scramble\Support\Type\Type; | ||
use Illuminate\Auth\AuthenticationException; | ||
use Illuminate\Support\Str; | ||
|
||
class AuthenticationExceptionToResponseExtension extends ExceptionToResponseExtension | ||
{ | ||
public function shouldHandle(Type $type) | ||
{ | ||
return $type instanceof ObjectType | ||
&& $type->isInstanceOf(AuthenticationException::class); | ||
} | ||
|
||
public function toResponse(Type $type) | ||
{ | ||
$responseBodyType = (new OpenApiTypes\ObjectType()) | ||
->addProperty( | ||
'message', | ||
(new OpenApiTypes\StringType()) | ||
->setDescription('Error overview.') | ||
) | ||
->setRequired(['message']); | ||
|
||
return Response::make(401) | ||
->description('Unauthenticated') | ||
->setContent( | ||
'application/json', | ||
Schema::fromType($responseBodyType) | ||
); | ||
} | ||
|
||
public function reference(ObjectType $type) | ||
{ | ||
return new Reference('responses', Str::start($type->name, '\\'), $this->components); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters