Skip to content

Commit 90744da

Browse files
committed
wip
1 parent 89bf8aa commit 90744da

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

config/config.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
return [
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
@@ -16,8 +28,8 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

src/LaravelStatelessSessionServiceProvider.php

+19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Binarcode\LaravelStatelessSession;
44

5+
use Binarcode\LaravelStatelessSession\Http\Middleware\StartStatelessSession;
56
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Support\Facades\Route;
8+
use Binarcode\LaravelStatelessSession\Http\Controllers\CsrfHeaderController;
69

710
class 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
}

0 commit comments

Comments
 (0)