Skip to content

Commit ac12090

Browse files
authored
feat: Configurable Route Paths (#436)
* Update composer.json * Update auth0.php * Update rector.php * Update Configuration.php * Update CallbackControllerAbstract.php * Update LoginControllerAbstract.php * Update LogoutControllerAbstract.php * Update CallbackControllerAbstract.php * Update rector.php * Update Configuration.php * Updates to router configuration * Router configuration updates * Update router configuration * Update auth0.php
1 parent 4482d34 commit ac12090

9 files changed

+229
-101
lines changed

config/auth0.php

+9
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,13 @@
5555
Configuration::CONFIG_TRANSIENT_STORAGE_ID => Configuration::get(Configuration::CONFIG_TRANSIENT_STORAGE_ID),
5656
],
5757
],
58+
59+
'routes' => [
60+
Configuration::CONFIG_ROUTE_INDEX => Configuration::get(Configuration::CONFIG_ROUTE_INDEX, '/'),
61+
Configuration::CONFIG_ROUTE_CALLBACK => Configuration::get(Configuration::CONFIG_ROUTE_CALLBACK, '/callback'),
62+
Configuration::CONFIG_ROUTE_LOGIN => Configuration::get(Configuration::CONFIG_ROUTE_LOGIN, '/login'),
63+
Configuration::CONFIG_ROUTE_AFTER_LOGIN => Configuration::get(Configuration::CONFIG_ROUTE_AFTER_LOGIN, '/'),
64+
Configuration::CONFIG_ROUTE_LOGOUT => Configuration::get(Configuration::CONFIG_ROUTE_LOGOUT, '/logout'),
65+
Configuration::CONFIG_ROUTE_AFTER_LOGOUT => Configuration::get(Configuration::CONFIG_ROUTE_AFTER_LOGOUT, '/'),
66+
],
5867
];

rector.php

+122-88
Large diffs are not rendered by default.

src/Configuration.php

+56
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ final class Configuration implements ConfigurationContract
148148
*/
149149
public const CONFIG_MANAGEMENT_TOKEN_CACHE = 'managementTokenCache';
150150

151+
/**
152+
* @var string
153+
*/
154+
public const CONFIG_NAMESPACE = 'auth0.';
155+
156+
/**
157+
* @var string
158+
*/
159+
public const CONFIG_NAMESPACE_ROUTES = 'auth0.routes.';
160+
151161
/**
152162
* @var string
153163
*/
@@ -173,6 +183,41 @@ final class Configuration implements ConfigurationContract
173183
*/
174184
public const CONFIG_RESPONSE_TYPE = 'responseType';
175185

186+
/**
187+
* @var string
188+
*/
189+
public const CONFIG_ROUTE_AFTER_LOGIN = 'afterLogin';
190+
191+
/**
192+
* @var string
193+
*/
194+
public const CONFIG_ROUTE_AFTER_LOGOUT = 'afterLogout';
195+
196+
/**
197+
* @var string
198+
*/
199+
public const CONFIG_ROUTE_BACKCHANNEL = 'backchannel';
200+
201+
/**
202+
* @var string
203+
*/
204+
public const CONFIG_ROUTE_CALLBACK = 'callback';
205+
206+
/**
207+
* @var string
208+
*/
209+
public const CONFIG_ROUTE_INDEX = 'index';
210+
211+
/**
212+
* @var string
213+
*/
214+
public const CONFIG_ROUTE_LOGIN = 'login';
215+
216+
/**
217+
* @var string
218+
*/
219+
public const CONFIG_ROUTE_LOGOUT = 'logout';
220+
176221
/**
177222
* @var string
178223
*/
@@ -435,6 +480,17 @@ public static function getPath(): string
435480
return self::$path;
436481
}
437482

483+
public static function string(string $key, ?string $default = null): ?string
484+
{
485+
$value = config($key, $default);
486+
487+
if (is_string($value)) {
488+
return $value;
489+
}
490+
491+
return null;
492+
}
493+
438494
public static function stringOrIntToIntOrNull(
439495
int | string $value,
440496
int | null $default = null,

src/Controllers/CallbackControllerAbstract.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use Auth0\Laravel\Auth\Guard;
88
use Auth0\Laravel\Entities\CredentialEntityContract;
9-
use Auth0\Laravel\Events;
109
use Auth0\Laravel\Events\{AuthenticationFailed, AuthenticationSucceeded};
1110
use Auth0\Laravel\Exceptions\ControllerException;
1211
use Auth0\Laravel\Exceptions\Controllers\CallbackControllerException;
1312
use Auth0\Laravel\Guards\GuardAbstract;
13+
use Auth0\Laravel\{Configuration, Events};
1414
use Illuminate\Auth\Events\{Attempting, Authenticated, Failed, Validated};
1515
use Illuminate\Contracts\Auth\Authenticatable;
1616
use Illuminate\Http\Request;
@@ -115,7 +115,7 @@ public function __invoke(
115115
}
116116

117117
if (! $success) {
118-
return redirect()->intended('/login');
118+
return redirect()->intended(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN, '/login'));
119119
}
120120

121121
$credential = ($guard instanceof Guard) ? $guard->find(Guard::SOURCE_SESSION) : $guard->find();
@@ -140,6 +140,6 @@ public function __invoke(
140140
}
141141
}
142142

143-
return redirect()->intended('/');
143+
return redirect()->intended(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_AFTER_LOGIN, config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_INDEX, '/')));
144144
}
145145
}

src/Controllers/LoginControllerAbstract.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use Auth0\Laravel\Auth\Guard;
88
use Auth0\Laravel\Entities\CredentialEntityContract;
9-
use Auth0\Laravel\Events;
109
use Auth0\Laravel\Events\LoginAttempting;
1110
use Auth0\Laravel\Exceptions\ControllerException;
1211
use Auth0\Laravel\Guards\GuardAbstract;
12+
use Auth0\Laravel\{Configuration, Events};
1313
use Illuminate\Http\Request;
1414
use Symfony\Component\HttpFoundation\Response;
1515

@@ -40,7 +40,15 @@ public function __invoke(
4040
$loggedIn ??= (($guard instanceof Guard) ? $guard->find(Guard::SOURCE_SESSION) : $guard->find()) instanceof CredentialEntityContract;
4141

4242
if ($loggedIn) {
43-
return redirect()->intended('/');
43+
return redirect()->intended(
44+
config(
45+
Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_AFTER_LOGIN,
46+
config(
47+
Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_INDEX,
48+
'/',
49+
),
50+
),
51+
);
4452
}
4553

4654
session()->regenerate(true);

src/Controllers/LogoutControllerAbstract.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Auth0\Laravel\Controllers;
66

77
use Auth0\Laravel\Auth\Guard;
8+
use Auth0\Laravel\Configuration;
89
use Auth0\Laravel\Entities\CredentialEntityContract;
910
use Auth0\Laravel\Exceptions\ControllerException;
1011
use Auth0\Laravel\Guards\GuardAbstract;
@@ -37,16 +38,20 @@ public function __invoke(
3738
$loggedIn = $guard->check() ? true : null;
3839
$loggedIn ??= (($guard instanceof Guard) ? $guard->find(Guard::SOURCE_SESSION) : $guard->find()) instanceof CredentialEntityContract;
3940

41+
$landing = Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_AFTER_LOGOUT);
42+
$landing ??= Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_INDEX);
43+
$landing ??= '/';
44+
4045
if ($loggedIn) {
4146
session()->invalidate();
4247

4348
$guard->logout(); /** @phpstan-ignore-line */
44-
$route = (string) url('/'); /** @phpstan-ignore-line */
49+
$route = (string) url($landing); /** @phpstan-ignore-line */
4550
$url = $guard->sdk()->authentication()->getLogoutLink($route);
4651

4752
return redirect()->away($url);
4853
}
4954

50-
return redirect()->intended('/');
55+
return redirect()->intended($landing);
5156
}
5257
}

src/ServiceAbstract.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ final public static function routes(
5555
string $authenticationGuard = 'auth0-session',
5656
): void {
5757
Route::group(['middleware' => ['web', 'guard:' . $authenticationGuard]], static function (): void {
58-
Route::get('/login', LoginController::class)->name('login');
59-
Route::get('/logout', LogoutController::class)->name('logout');
60-
Route::get('/callback', CallbackController::class)->name('callback');
58+
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN) ?? '/login', LoginController::class)->name('login');
59+
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGOUT) ?? '/logout', LogoutController::class)->name('logout');
60+
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_CALLBACK) ?? '/callback', CallbackController::class)->name('callback');
6161
});
6262
}
6363
}

src/ServiceProviderAbstract.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ final public function registerRoutes(): void
220220
{
221221
if (true === config('auth0.registerAuthenticationRoutes')) {
222222
Route::group(['middleware' => 'web'], static function (): void {
223-
Route::get('/login', LoginController::class)->name('login');
224-
Route::get('/logout', LogoutController::class)->name('logout');
225-
Route::get('/callback', CallbackController::class)->name('callback');
223+
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN) ?? '/login', LoginController::class)->name('login');
224+
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGOUT) ?? '/logout', LogoutController::class)->name('logout');
225+
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_CALLBACK) ?? '/callback', CallbackController::class)->name('callback');
226226
});
227227
}
228228
}

tests/Unit/ConfigurationTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,19 @@
148148
->toBeInt()
149149
->toEqual(123);
150150
});
151+
152+
test('string() behaves as expected', function (): void {
153+
config(['test2' => [
154+
'testInteger' => 123,
155+
'testString' => '123',
156+
]]);
157+
158+
define('AUTH0_OVERRIDE_CONFIGURATION_STRING_METHOD', 'test2');
159+
160+
expect(Configuration::string('test2.testInteger'))
161+
->toBeNull();
162+
163+
expect(Configuration::string('test2.testString'))
164+
->toBeString()
165+
->toEqual('123');
166+
});

0 commit comments

Comments
 (0)