-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLaravelStatelessSessionServiceProvider.php
executable file
·70 lines (60 loc) · 2 KB
/
LaravelStatelessSessionServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Binarcode\LaravelStatelessSession;
use Binarcode\LaravelStatelessSession\Http\Controllers\CsrfHeaderController;
use Binarcode\LaravelStatelessSession\Http\Middleware\StatelessStartSession;
use Binarcode\LaravelStatelessSession\Http\Middleware\StatelessVerifyCsrfToken;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class LaravelStatelessSessionServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
$this->defineRoutes();
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/config.php' => config_path('stateless.php'),
], 'config');
}
}
/**
* Register the application services.
*/
public function register()
{
$this->registerSessionManager();
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'stateless');
$this->app->singleton('laravel-stateless-session', function () {
return new LaravelStatelessSession;
});
}
/**
* We have to override this singleton because we want to pass
* the name to the session header from another configuration key:
* session.header
*/
protected function registerSessionManager()
{
$this->app->singleton('session', function ($app) {
return new SessionManager($app);
});
}
protected function defineRoutes()
{
if ($this->app->routesAreCached()) {
return;
}
Route::group(['prefix' => config('stateless.prefix', 'api'), 'middleware' => config('stateless.middleware'),], function () {
Route::get(
'/csrf-header',
CsrfHeaderController::class . '@show'
)->middleware([
StatelessStartSession::class,
StatelessVerifyCsrfToken::class,
]);
});
}
}