2
2
3
3
namespace ChrisReedIO \Socialment ;
4
4
5
+ use ChrisReedIO \Socialment \Http \Middleware \VerifySpaCsrfToken ;
5
6
use ChrisReedIO \Socialment \Testing \TestsSocialment ;
6
7
use Filament \Support \Assets \Asset ;
7
8
use Filament \Support \Facades \FilamentAsset ;
8
9
use Filament \Support \Facades \FilamentIcon ;
9
10
use Illuminate \Filesystem \Filesystem ;
11
+ use Illuminate \Foundation \Http \Middleware \VerifyCsrfToken ;
10
12
use Illuminate \Support \Facades \Route ;
11
13
use Livewire \Features \SupportTesting \Testable ;
12
14
use Spatie \LaravelPackageTools \Commands \InstallCommand ;
13
15
use Spatie \LaravelPackageTools \Package ;
14
16
use Spatie \LaravelPackageTools \PackageServiceProvider ;
15
17
18
+ use function array_merge ;
19
+ use function config ;
20
+
16
21
class SocialmentServiceProvider extends PackageServiceProvider
17
22
{
18
23
public static string $ name = 'socialment ' ;
19
24
20
25
public static string $ viewNamespace = 'socialment ' ;
21
26
27
+ public static string $ middlewareGroupName = 'web_spa ' ;
28
+
22
29
public function configurePackage (Package $ package ): void
23
30
{
24
31
/*
@@ -59,18 +66,28 @@ public function configurePackage(Package $package): void
59
66
public function packageRegistered (): void
60
67
{
61
68
$ this ->app ->singleton (SocialmentPlugin::class, fn () => new SocialmentPlugin ());
62
- }
63
69
64
- public function packageBooted (): void
65
- {
66
- Route::macro ('spaAuth ' , function (string $ prefix = 'spa ' ) {
70
+ Route::macro ('spaInit ' , function (string $ prefix = 'spa ' ) {
67
71
$ namePrefix = 'socialment.spa. ' ;
68
72
$ namePrefix .= ($ prefix === 'spa ' ) ? 'default. ' : "{$ prefix }. " ;
69
73
70
- Route::middleware ('web ' )
71
- ->prefix ($ prefix )
72
- ->as ($ namePrefix )
73
- ->group (__DIR__ . '/../routes/spa.php ' );
74
+ $ useCustomCsrf = config ('socialment.spa.cookies.csrf.custom ' );
75
+
76
+ $ dashboardSpaMiddleware = [
77
+ \Illuminate \Cookie \Middleware \EncryptCookies::class,
78
+ \Illuminate \Cookie \Middleware \AddQueuedCookiesToResponse::class,
79
+ \Illuminate \Session \Middleware \StartSession::class,
80
+ \Illuminate \View \Middleware \ShareErrorsFromSession::class,
81
+ $ useCustomCsrf
82
+ ? \ChrisReedIO \Socialment \Http \Middleware \VerifySpaCsrfToken::class
83
+ : \Illuminate \Foundation \Http \Middleware \VerifyCsrfToken::class,
84
+ \Illuminate \Routing \Middleware \SubstituteBindings::class,
85
+ ];
86
+
87
+ Route::middlewareGroup (SocialmentServiceProvider::$ middlewareGroupName , $ dashboardSpaMiddleware );
88
+
89
+ // Route::middleware([SocialmentServiceProvider::$middlewareGroupName])
90
+ // ->group(__DIR__.'/../routes/spa.php');
74
91
75
92
// Now add this to the cors paths
76
93
config ([
@@ -83,8 +100,103 @@ public function packageBooted(): void
83
100
config ([
84
101
'cors.supports_credentials ' => true ,
85
102
]);
103
+ // collect(Route::getRoutes())
104
+ // ->filter(fn ($route) => str_starts_with($route->uri(), $prefix))
105
+ // ->each(function (\Illuminate\Routing\Route $route) use ($dashboardSpaMiddleware) {
106
+ // $route->action['middleware'] = array_values(array_diff($route->action['middleware'], ['web']));
107
+ // $route->middleware($dashboardSpaMiddleware);
108
+ // });
86
109
});
87
110
111
+ Route::macro ('spaAuth ' , function (string $ prefix = 'spa ' ) {
112
+ $ namePrefix = 'socialment.spa. ' ;
113
+ $ namePrefix .= ($ prefix === 'spa ' ) ? 'default. ' : "{$ prefix }. " ;
114
+
115
+ // $useCustomCsrf = config('socialment.spa.cookies.csrf.custom');
116
+ // dd($useCustomCsrf);
117
+
118
+
119
+ // $dashboardSpaMiddleware = [
120
+ // \Illuminate\Cookie\Middleware\EncryptCookies::class,
121
+ // \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
122
+ // \Illuminate\Session\Middleware\StartSession::class,
123
+ // \Illuminate\View\Middleware\ShareErrorsFromSession::class,
124
+ // $useCustomCsrf
125
+ // ? \ChrisReedIO\Socialment\Http\Middleware\VerifySpaCsrfToken::class
126
+ // : \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
127
+ // \Illuminate\Routing\Middleware\SubstituteBindings::class,
128
+ // ];
129
+ //
130
+ // // dd($dashboardSpaMiddleware);
131
+ //
132
+ // Route::middlewareGroup(SocialmentServiceProvider::$middlewareGroupName, $dashboardSpaMiddleware);
133
+
134
+ Route::middleware ([SocialmentServiceProvider::$ middlewareGroupName ])
135
+ // ->prefix($prefix)
136
+ // ->as($namePrefix)
137
+ ->group (__DIR__ .'/../routes/spa.php ' );
138
+
139
+ // Now add this to the cors paths
140
+ // config([
141
+ // 'cors.paths' => array_merge(config('cors.paths'), [
142
+ // "{$prefix}/*",
143
+ // ]),
144
+ // ]);
145
+
146
+ // Set the supports_credentials flag or the frontend can't send the goodies
147
+ // config([
148
+ // 'cors.supports_credentials' => true,
149
+ // ]);
150
+
151
+ // Apply middleware conditionally to all routes with the '/dashboard' prefix
152
+ // Route::middlewareGroup('spa', [
153
+ // \Illuminate\Cookie\Middleware\EncryptCookies::class,
154
+ // \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
155
+ // \Illuminate\Session\Middleware\StartSession::class,
156
+ // \Illuminate\View\Middleware\ShareErrorsFromSession::class,
157
+ // config('socialment.spa.cookies.csrf.custom')
158
+ // ? VerifySpaCsrfToken::class
159
+ // : VerifyCsrfToken::class,
160
+ // \Illuminate\Routing\Middleware\SubstituteBindings::class,
161
+ // ]);
162
+
163
+
164
+ // Apply 'dashboard_spa' group to any routes prefixed with '/dashboard'
165
+ // Route::middleware('spa')
166
+ // ->prefix('dashboard')
167
+ // ->group(function () {
168
+ // //
169
+ // });
170
+ // Use the `Route` facade to get all routes and modify them if they start with `/dashboard`
171
+ // collect(Route::getRoutes())
172
+ // ->filter(fn ($route) => str_starts_with($route->uri(), $prefix))
173
+ // ->each(function (\Illuminate\Routing\Route $route) use ($dashboardSpaMiddleware) {
174
+ // // if ($route->uri() === 'dashboard/me') {
175
+ // // dump($route);
176
+ // // }
177
+ // // dump($route);
178
+ // // Replace the middleware stack for this route
179
+ // // Unset any 'values' of 'web' in the action's middleware
180
+ // $route->action['middleware'] = array_values(array_diff($route->action['middleware'], ['web']));
181
+ // // Insert our middleware group name at the start of the array
182
+ // // array_unshift($route->action['middleware'], SocialmentServiceProvider::$middlewareGroupName);
183
+ // // $route->middleware(SocialmentServiceProvider::$middlewareGroupName);
184
+ // $route->middleware($dashboardSpaMiddleware);
185
+ // // if ($route->uri() === 'dashboard/me') {
186
+ // // dump($route);
187
+ // // $middleware = $route->gatherMiddleware();
188
+ // // dd($middleware);
189
+ // // dd('done');
190
+ // // }
191
+ // // }
192
+ // });
193
+ // dd('done');
194
+ });
195
+
196
+ }
197
+
198
+ public function packageBooted (): void
199
+ {
88
200
// Asset Registration
89
201
FilamentAsset::register (
90
202
$ this ->getAssets (),
@@ -101,7 +213,7 @@ public function packageBooted(): void
101
213
102
214
// Handle Stubs
103
215
if (app ()->runningInConsole ()) {
104
- foreach (app (Filesystem::class)->files (__DIR__ . '/../stubs/ ' ) as $ file ) {
216
+ foreach (app (Filesystem::class)->files (__DIR__ . '/../stubs/ ' ) as $ file ) {
105
217
$ this ->publishes ([
106
218
$ file ->getRealPath () => base_path ("stubs/socialment/ {$ file ->getFilename ()}" ),
107
219
], 'socialment-stubs ' );
0 commit comments