Skip to content

Commit 460edf5

Browse files
authored
Merge pull request #266 from leoralph/main
Add config to set auth cookie key name
2 parents c2b0aa7 + c72633a commit 460edf5

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ You can find and compare releases at the GitHub release page.
1010

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

1415
### Removed
1516

config/config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@
254254

255255
'decrypt_cookies' => false,
256256

257+
/*
258+
|--------------------------------------------------------------------------
259+
| Cookie key name
260+
|--------------------------------------------------------------------------
261+
|
262+
| Specify the cookie key name that you would like to use for the cookie token.
263+
|
264+
*/
265+
266+
'cookie_key_name' => 'token',
267+
257268
/*
258269
|--------------------------------------------------------------------------
259270
| Providers

docs/quick-start.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,18 @@ There are a number of ways to send the token via http:
221221

222222
**Cookies**
223223

224+
By default, you can send a token via a cookie. You can customize the cookie name within the `jwt.cookie_name` configuration.
225+
226+
```php
227+
// config/jwt.php
228+
229+
return [
230+
// ...
231+
'cookie_name' => 'token',
232+
// ...
233+
];
234+
```
235+
236+
When making requests, the token will be automatically read from the specified cookie, the default key name is `token`.
237+
224238
**Laravel route parameter**

src/Providers/JWT/Lcobucci.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(
7171
$secret,
7272
$algo,
7373
array $keys,
74-
$config = null
74+
$config = null,
7575
) {
7676
parent::__construct($secret, $algo, $keys);
7777
$this->generateConfig($config);

src/Providers/LaravelServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ public function boot()
3434

3535
$this->extendAuthGuard();
3636

37+
$config = $this->app->make('config');
38+
3739
$this->app['tymon.jwt.parser']->addParser([
3840
new RouteParams(),
39-
new Cookies($this->app->make('config')->get('jwt.decrypt_cookies')),
41+
(new Cookies(
42+
$config->get('jwt.decrypt_cookies'),
43+
))->setKey($config->get('jwt.cookie_key_name', 'token')),
4044
]);
4145

4246
if (isset($_SERVER['LARAVEL_OCTANE'])) {

tests/DefaultConfigValuesTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ public function testProvidersShouldBeSet()
109109
$this->assertEquals(AuthIlluminate::class, $this->configuration['providers']['auth']);
110110
$this->assertEquals(StorageIlluminate::class, $this->configuration['providers']['storage']);
111111
}
112+
113+
public function testCookieKeyNameShouldBeSet()
114+
{
115+
$this->assertEquals('token', $this->configuration['cookie_key_name']);
116+
}
112117
}

0 commit comments

Comments
 (0)