|
4 | 4 |
|
5 | 5 | use Closure; |
6 | 6 | use Fleetbase\Build\Expansion; |
| 7 | +use Fleetbase\Http\Controllers\Internal\v1\AuthController; |
| 8 | +use Fleetbase\Http\Middleware\ThrottleRequests; |
7 | 9 | use Fleetbase\Routing\RESTRegistrar; |
8 | 10 | use Illuminate\Routing\PendingResourceRegistration; |
| 11 | +use Illuminate\Routing\Router; |
9 | 12 | use Illuminate\Support\Str; |
10 | 13 |
|
11 | 14 | class Route implements Expansion |
@@ -37,7 +40,7 @@ public function fleetbaseRestRoutes() |
37 | 40 | * @return PendingResourceRegistration |
38 | 41 | */ |
39 | 42 | return function (string $name, $controller = null, $options = [], ?\Closure $callback = null) { |
40 | | - /** @var \Illuminate\Routing\Router $this */ |
| 43 | + /** @var Router $this */ |
41 | 44 | if (is_callable($controller) && $callback === null) { |
42 | 45 | $callback = $controller; |
43 | 46 | $controller = null; |
@@ -65,7 +68,7 @@ public function fleetbaseRestRoutes() |
65 | 68 | public function fleetbaseRoutes() |
66 | 69 | { |
67 | 70 | return function (string $name, callable|array|null $registerFn = null, $options = [], $controller = null) { |
68 | | - /** @var \Illuminate\Routing\Router $this */ |
| 71 | + /** @var Router $this */ |
69 | 72 | if (is_array($registerFn) && !empty($registerFn) && empty($options)) { |
70 | 73 | $options = $registerFn; |
71 | 74 | } |
@@ -112,50 +115,44 @@ function ($router) use ($registerFn, $make, $controller) { |
112 | 115 | }; |
113 | 116 | } |
114 | 117 |
|
115 | | - public function fleetbaseAuthRoutes() |
| 118 | + public function fleetbaseAuthRoutes(): \Closure |
116 | 119 | { |
117 | | - return function (?callable $registerFn = null, ?callable $registerProtectedFn = null) { |
118 | | - /** @var \Illuminate\Routing\Router $this */ |
119 | | - return $this->group( |
120 | | - ['prefix' => 'auth'], |
121 | | - function ($router) use ($registerFn, $registerProtectedFn) { |
122 | | - $router->group( |
123 | | - ['middleware' => ['throttle:10,1', \Spatie\ResponseCache\Middlewares\DoNotCacheResponse::class]], |
124 | | - function ($router) use ($registerFn) { |
125 | | - $router->post('login', 'AuthController@login'); |
126 | | - $router->post('sign-up', 'AuthController@signUp'); |
127 | | - $router->post('logout', 'AuthController@logout'); |
128 | | - $router->post('get-magic-reset-link', 'AuthController@createPasswordReset'); |
129 | | - $router->post('reset-password', 'AuthController@resetPassword'); |
130 | | - $router->post('create-verification-session', 'AuthController@createVerificationSession'); |
131 | | - $router->post('validate-verification-session', 'AuthController@validateVerificationSession'); |
132 | | - $router->post('send-verification-email', 'AuthController@sendVerificationEmail'); |
133 | | - $router->post('verify-email', 'AuthController@verifyEmail'); |
134 | | - $router->get('validate-verification', 'AuthController@validateVerificationCode'); |
135 | | - |
136 | | - if (is_callable($registerFn)) { |
137 | | - $registerFn($router); |
138 | | - } |
139 | | - } |
140 | | - ); |
141 | | - |
142 | | - $router->group( |
143 | | - ['middleware' => ['fleetbase.protected', \Spatie\ResponseCache\Middlewares\DoNotCacheResponse::class]], |
144 | | - function ($router) use ($registerProtectedFn) { |
145 | | - $router->post('switch-organization', 'AuthController@switchOrganization'); |
146 | | - $router->post('join-organization', 'AuthController@joinOrganization'); |
147 | | - $router->post('create-organization', 'AuthController@createOrganization'); |
148 | | - $router->get('session', 'AuthController@session'); |
149 | | - $router->get('organizations', 'AuthController@getUserOrganizations'); |
150 | | - $router->get('services', 'AuthController@services'); |
151 | | - |
152 | | - if (is_callable($registerProtectedFn)) { |
153 | | - $registerProtectedFn($router); |
154 | | - } |
155 | | - } |
156 | | - ); |
157 | | - } |
158 | | - ); |
| 120 | + return function (?string $authControllerClass = null, ?callable $registerFn = null, ?callable $registerProtectedFn = null) { |
| 121 | + $authControllerClass ??= AuthController::class; |
| 122 | + /** @var Router $this */ |
| 123 | + $this->group(['prefix' => 'auth'], function (Router $router) use ($authControllerClass, $registerFn, $registerProtectedFn) { |
| 124 | + // Public auth routes with throttle |
| 125 | + $router->group(['middleware' => [ThrottleRequests::class]], function (Router $router) use ($authControllerClass, $registerFn) { |
| 126 | + $router->post('login', [$authControllerClass, 'login']); |
| 127 | + $router->post('sign-up', [$authControllerClass, 'signUp']); |
| 128 | + $router->post('logout', [$authControllerClass, 'logout']); |
| 129 | + $router->post('get-magic-reset-link', [$authControllerClass, 'createPasswordReset']); |
| 130 | + $router->post('reset-password', [$authControllerClass, 'resetPassword']); |
| 131 | + $router->post('create-verification-session', [$authControllerClass, 'createVerificationSession']); |
| 132 | + $router->post('validate-verification-session', [$authControllerClass, 'validateVerificationSession']); |
| 133 | + $router->post('send-verification-email', [$authControllerClass, 'sendVerificationEmail']); |
| 134 | + $router->post('verify-email', [$authControllerClass, 'verifyEmail']); |
| 135 | + $router->get('validate-verification', [$authControllerClass, 'validateVerificationCode']); |
| 136 | + |
| 137 | + if (is_callable($registerFn)) { |
| 138 | + $registerFn($router); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + // Protected auth routes |
| 143 | + $router->group(['middleware' => ['fleetbase.protected']], function (Router $router) use ($authControllerClass, $registerProtectedFn) { |
| 144 | + $router->post('switch-organization', [$authControllerClass, 'switchOrganization']); |
| 145 | + $router->post('join-organization', [$authControllerClass, 'joinOrganization']); |
| 146 | + $router->post('create-organization', [$authControllerClass, 'createOrganization']); |
| 147 | + $router->get('session', [$authControllerClass, 'session']); |
| 148 | + $router->get('organizations', [$authControllerClass, 'getUserOrganizations']); |
| 149 | + $router->get('services', [$authControllerClass, 'services']); |
| 150 | + |
| 151 | + if (is_callable($registerProtectedFn)) { |
| 152 | + $registerProtectedFn($router); |
| 153 | + } |
| 154 | + }); |
| 155 | + }); |
159 | 156 | }; |
160 | 157 | } |
161 | 158 |
|
|
0 commit comments