Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config to set auth cookie key name #266

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}
}