Skip to content

Commit bb842a5

Browse files
toitzidunglas
andauthored
[Laravel] Add Swagger Authentication Documentation (#1991)
* fix(laravel): installation add authentication instructions for swagger ui in laravel * Update index.md --------- Co-authored-by: Kévin Dunglas <[email protected]>
1 parent 145d323 commit bb842a5

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

laravel/index.md

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ With API Platform, you can:
2222
<!-- * benefits from native HTTP cache (with automatic invalidation) -->
2323
* boost your app with [Octane](https://laravel.com/docs/octane) and [FrankenPHP](https://frankenphp.dev) (the default Octane engine, also created by Kévin)
2424
* [decouple your API from your models](../core/state-providers.md) and implement patterns such as CQRS
25-
* test your API using convenient ad-hoc assertions that works with Pest and PHPUnit
25+
* test your API using convenient ad-hoc assertions that work with Pest and PHPUnit
2626

2727
Let's discover how to use API Platform with Laravel!
2828

@@ -44,10 +44,20 @@ cd my-api-platform-laravel-app
4444
In your Laravel project, install the API Platform integration for Laravel:
4545

4646
```console
47-
composer require api-platform/laravel:^4
47+
composer require api-platform/laravel
4848
```
4949

50-
If it's not already done, run `php artisan serve` to start the built-in web server.
50+
After installing API Platform, publish its assets and config:
51+
52+
```console
53+
php artisan api-platform:install
54+
```
55+
56+
If it's not already done, start the built-in web server:
57+
58+
```console
59+
php artisan serve
60+
```
5161

5262
Open `http://127.0.0.1:8000/api/`, your API is already active and documented... but empty!
5363

@@ -126,7 +136,7 @@ namespace App\Models;
126136
}
127137
```
128138

129-
Open `http://127.0.0.1:8000/api/`, tadam, your API is ready and **entirely functionnal** 🎉:
139+
Open `http://127.0.0.1:8000/api/`, tadam, your API is ready and **entirely functional** 🎉:
130140

131141
![Basic REST API](images/basic-rest.png)
132142

@@ -271,7 +281,7 @@ class Book extends Model
271281

272282
## Relations and Nested Resources
273283

274-
Let's replace our author column by a relation to a new `author` table:
284+
Let's replace our author column with a relation to a new `author` table:
275285

276286
```patch
277287
public function up(): void
@@ -295,7 +305,7 @@ Let's replace our author column by a relation to a new `author` table:
295305
}
296306
```
297307

298-
By doing so, API Platform will automatically handle links to that relation using your prefered format (JSON:API, JSON-LD etc)
308+
By doing so, API Platform will automatically handle links to that relation using your preferred format (JSON:API, JSON-LD, etc)
299309
and when we request a Book we obtain:
300310

301311
```json
@@ -559,7 +569,49 @@ It also natively supports:
559569
* [Laravel Passport](https://laravel.com/docs/passport), a full OAuth 2 server
560570
* [Laravel Socialite](https://laravel.com/docs/socialite), OAuth providers including Facebook, X, LinkedIn, Google, GitHub, GitLab, Bitbucket, and Slack
561571

562-
Follow the official instructions of the tool(s) you want to use.
572+
Follow the official instructions for the tool(s) you want to use.
573+
574+
### Login With Swagger UI
575+
576+
In Swagger UI, you can authenticate your requests using the `Authorize` button in the top right corner.
577+
To use it, you need to add some configuration in the `config/api-platform.php` file.
578+
579+
Here is an example of how to configure API key authentication:
580+
581+
```php
582+
// config/api-platform.php
583+
'swagger_ui' => [
584+
'enabled' => true,
585+
'apiKeys' => [
586+
'api' => [
587+
'type' => 'header',
588+
'name' => 'X-API-Key'
589+
]
590+
]
591+
]
592+
```
593+
594+
Or if you are using Laravel Passport (or any other OAuth server):
595+
596+
```php
597+
// config/api-platform.php
598+
'swagger_ui' => [
599+
'enabled' => true,
600+
'oauth' => [
601+
'enabled' => true,
602+
'type' => 'oauth2',
603+
'flow' => 'authorizationCode',
604+
'tokenUrl' => '<oauth_token_endpoint>',
605+
'authorizationUrl' =>'<oauth_authorization_endpoint>',
606+
'refreshUrl' => '<oauth_refresh_endpoint>',
607+
'scopes' => ['scope' => 'Description of the scope'],
608+
'pkce' => true,
609+
]
610+
]
611+
```
612+
613+
A combination of both is also possible.
614+
For more information, you can also check the [Swagger UI documentation](https://swagger.io/docs/specification/authentication/).
563615

564616
### Middlewares
565617

0 commit comments

Comments
 (0)