Skip to content

Commit

Permalink
Merge pull request #266 from leoralph/main
Browse files Browse the repository at this point in the history
Add config to set auth cookie key name
  • Loading branch information
Messhias authored Sep 27, 2024
2 parents c2b0aa7 + c72633a commit 460edf5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ You can find and compare releases at the GitHub release page.

### Added
- Fixes #259 - Can't logout with an expired token
- Add `cookie_key_name` config to customize cookie name for authentication

### Removed

Expand Down
11 changes: 11 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,17 @@

'decrypt_cookies' => false,

/*
|--------------------------------------------------------------------------
| Cookie key name
|--------------------------------------------------------------------------
|
| Specify the cookie key name that you would like to use for the cookie token.
|
*/

'cookie_key_name' => 'token',

/*
|--------------------------------------------------------------------------
| Providers
Expand Down
14 changes: 14 additions & 0 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,18 @@ There are a number of ways to send the token via http:

**Cookies**

By default, you can send a token via a cookie. You can customize the cookie name within the `jwt.cookie_name` configuration.

```php
// config/jwt.php

return [
// ...
'cookie_name' => 'token',
// ...
];
```

When making requests, the token will be automatically read from the specified cookie, the default key name is `token`.

**Laravel route parameter**
2 changes: 1 addition & 1 deletion src/Providers/JWT/Lcobucci.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(
$secret,
$algo,
array $keys,
$config = null
$config = null,
) {
parent::__construct($secret, $algo, $keys);
$this->generateConfig($config);
Expand Down
6 changes: 5 additions & 1 deletion src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ public function boot()

$this->extendAuthGuard();

$config = $this->app->make('config');

$this->app['tymon.jwt.parser']->addParser([
new RouteParams(),
new Cookies($this->app->make('config')->get('jwt.decrypt_cookies')),
(new Cookies(
$config->get('jwt.decrypt_cookies'),
))->setKey($config->get('jwt.cookie_key_name', 'token')),
]);

if (isset($_SERVER['LARAVEL_OCTANE'])) {
Expand Down
5 changes: 5 additions & 0 deletions tests/DefaultConfigValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ public function testProvidersShouldBeSet()
$this->assertEquals(AuthIlluminate::class, $this->configuration['providers']['auth']);
$this->assertEquals(StorageIlluminate::class, $this->configuration['providers']['storage']);
}

public function testCookieKeyNameShouldBeSet()
{
$this->assertEquals('token', $this->configuration['cookie_key_name']);
}
}

0 comments on commit 460edf5

Please sign in to comment.