File tree Expand file tree Collapse file tree 3 files changed +51
-2
lines changed
Expand file tree Collapse file tree 3 files changed +51
-2
lines changed Original file line number Diff line number Diff line change 44
55return [
66
7+ /*
8+ |--------------------------------------------------------------------------
9+ | The router prefix
10+ |--------------------------------------------------------------------------
11+ |
12+ | This will be the prefix for the `/csrf-header` GET route.
13+ | E.g. prefix = 'api' => than you can get the header token via: `/api/csrf-header
14+ |
15+ */
16+
17+ 'prefix ' => env ('STATELESS_PREFIX ' , 'api ' ),
18+
719 /*
820 |--------------------------------------------------------------------------
921 | Header Cookie Name
1628 */
1729
1830 'header ' => env (
19- 'SESSION_HEADER ' ,
20- Str::upper (Str::slug (env ('APP_NAME ' , 'laravel ' ), '- ' ). '-session ' )
31+ 'STATELESS_SESSION_HEADER ' ,
32+ Str::upper (Str::slug (env ('APP_NAME ' , 'laravel ' ), '- ' ) . '-session ' )
2133 ),
2234
2335];
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Binarcode \LaravelStatelessSession \Http \Controllers ;
4+
5+ use Illuminate \Http \Response ;
6+
7+ class CsrfHeaderController
8+ {
9+ /**
10+ * Return an empty response simply to trigger the storage of the CSRF cookie in the browser.
11+ *
12+ * @return \Illuminate\Http\Response
13+ */
14+ public function show ()
15+ {
16+ return new Response ('' , 204 );
17+ }
18+ }
Original file line number Diff line number Diff line change 22
33namespace Binarcode \LaravelStatelessSession ;
44
5+ use Binarcode \LaravelStatelessSession \Http \Middleware \StartStatelessSession ;
56use Illuminate \Support \ServiceProvider ;
7+ use Illuminate \Support \Facades \Route ;
8+ use Binarcode \LaravelStatelessSession \Http \Controllers \CsrfHeaderController ;
69
710class LaravelStatelessSessionServiceProvider extends ServiceProvider
811{
@@ -11,6 +14,8 @@ class LaravelStatelessSessionServiceProvider extends ServiceProvider
1114 */
1215 public function boot ()
1316 {
17+ $ this ->defineRoutes ();
18+
1419 if ($ this ->app ->runningInConsole ()) {
1520 $ this ->publishes ([
1621 __DIR__ .'/../config/config.php ' => config_path ('stateless.php ' ),
@@ -44,4 +49,18 @@ protected function registerSessionManager()
4449 return new SessionManager ($ app );
4550 });
4651 }
52+
53+ protected function defineRoutes ()
54+ {
55+ if ($ this ->app ->routesAreCached ()) {
56+ return ;
57+ }
58+
59+ Route::group (['prefix ' => config ('stateless.prefix ' , 'api ' )], function () {
60+ Route::get (
61+ '/csrf-header ' ,
62+ CsrfHeaderController::class.'@show '
63+ )->middleware (StartStatelessSession::class);
64+ });
65+ }
4766}
You can’t perform that action at this time.
0 commit comments