Skip to content

Commit ee581c9

Browse files
feat: redirection customization (#298)
* feat: redirection customization * fix redirects default * formatting * fix Co-authored-by: Taylor Otwell <[email protected]>
1 parent c6db8cc commit ee581c9

12 files changed

+39
-16
lines changed

config/fortify.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
'limiters' => [
1616
'login' => null,
1717
],
18+
'redirects' => [
19+
'login' => null,
20+
'logout' => null,
21+
'password-confirmation' => null,
22+
'register' => null,
23+
'email-verification' => null,
24+
],
1825
'features' => [
1926
Features::registration(),
2027
Features::resetPasswords(),

src/Fortify.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ public static function email()
6565
return config('fortify.email', 'email');
6666
}
6767

68+
/**
69+
* Get a completion redirect path for a specific feature.
70+
*
71+
* @param string $redirect
72+
* @return string
73+
*/
74+
public static function redirects(string $redirect, $default = null)
75+
{
76+
return config('fortify.redirects.'.$redirect) ?? $default ?? config('fortify.home');
77+
}
78+
6879
/**
6980
* Register the views for Fortify using conventional names under the given namespace.
7081
*

src/Http/Controllers/EmailVerificationNotificationController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\JsonResponse;
66
use Illuminate\Http\Request;
77
use Illuminate\Routing\Controller;
8+
use Laravel\Fortify\Fortify;
89

910
class EmailVerificationNotificationController extends Controller
1011
{
@@ -19,7 +20,7 @@ public function store(Request $request)
1920
if ($request->user()->hasVerifiedEmail()) {
2021
return $request->wantsJson()
2122
? new JsonResponse('', 204)
22-
: redirect()->intended(config('fortify.home'));
23+
: redirect()->intended(Fortify::redirects('email-verification'));
2324
}
2425

2526
$request->user()->sendEmailVerificationNotification();

src/Http/Controllers/EmailVerificationPromptController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Routing\Controller;
77
use Laravel\Fortify\Contracts\VerifyEmailViewResponse;
8+
use Laravel\Fortify\Fortify;
89

910
class EmailVerificationPromptController extends Controller
1011
{
@@ -17,7 +18,7 @@ class EmailVerificationPromptController extends Controller
1718
public function __invoke(Request $request)
1819
{
1920
return $request->user()->hasVerifiedEmail()
20-
? redirect()->intended(config('fortify.home'))
21+
? redirect()->intended(Fortify::redirects('email-verification'))
2122
: app(VerifyEmailViewResponse::class);
2223
}
2324
}

src/Http/Controllers/VerifyEmailController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Auth\Events\Verified;
66
use Illuminate\Http\JsonResponse;
77
use Illuminate\Routing\Controller;
8+
use Laravel\Fortify\Fortify;
89
use Laravel\Fortify\Http\Requests\VerifyEmailRequest;
910

1011
class VerifyEmailController extends Controller
@@ -20,7 +21,7 @@ public function __invoke(VerifyEmailRequest $request)
2021
if ($request->user()->hasVerifiedEmail()) {
2122
return $request->wantsJson()
2223
? new JsonResponse('', 204)
23-
: redirect()->intended(config('fortify.home').'?verified=1');
24+
: redirect()->intended(Fortify::redirects('email-verification').'?verified=1');
2425
}
2526

2627
if ($request->user()->markEmailAsVerified()) {
@@ -29,6 +30,6 @@ public function __invoke(VerifyEmailRequest $request)
2930

3031
return $request->wantsJson()
3132
? new JsonResponse('', 202)
32-
: redirect()->intended(config('fortify.home').'?verified=1');
33+
: redirect()->intended(Fortify::redirects('email-verification').'?verified=1');
3334
}
3435
}

src/Http/Responses/FailedPasswordConfirmationResponse.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Laravel\Fortify\Http\Responses;
44

5-
use Illuminate\Http\Response;
65
use Illuminate\Validation\ValidationException;
76
use Laravel\Fortify\Contracts\FailedPasswordConfirmationResponse as FailedPasswordConfirmationResponseContract;
87

@@ -24,6 +23,6 @@ public function toResponse($request)
2423
]);
2524
}
2625

27-
return redirect()->back()->withErrors(['password' => $message]);
26+
return back()->withErrors(['password' => $message]);
2827
}
2928
}

src/Http/Responses/FailedPasswordResetResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function toResponse($request)
3939
]);
4040
}
4141

42-
return redirect()->back()
43-
->withInput($request->only('email'))
44-
->withErrors(['email' => trans($this->status)]);
42+
return back()
43+
->withInput($request->only('email'))
44+
->withErrors(['email' => trans($this->status)]);
4545
}
4646
}

src/Http/Responses/LoginResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Fortify\Http\Responses;
44

55
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
6+
use Laravel\Fortify\Fortify;
67

78
class LoginResponse implements LoginResponseContract
89
{
@@ -16,6 +17,6 @@ public function toResponse($request)
1617
{
1718
return $request->wantsJson()
1819
? response()->json(['two_factor' => false])
19-
: redirect()->intended(config('fortify.home'));
20+
: redirect()->intended(Fortify::redirects('login'));
2021
}
2122
}

src/Http/Responses/LogoutResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\JsonResponse;
66
use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract;
7+
use Laravel\Fortify\Fortify;
78

89
class LogoutResponse implements LogoutResponseContract
910
{
@@ -17,6 +18,6 @@ public function toResponse($request)
1718
{
1819
return $request->wantsJson()
1920
? new JsonResponse('', 204)
20-
: redirect('/');
21+
: redirect(Fortify::redirects('logout', '/'));
2122
}
2223
}

src/Http/Responses/PasswordConfirmedResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Laravel\Fortify\Http\Responses;
44

55
use Illuminate\Http\JsonResponse;
6-
use Illuminate\Http\Response;
76
use Laravel\Fortify\Contracts\PasswordConfirmedResponse as PasswordConfirmedResponseContract;
7+
use Laravel\Fortify\Fortify;
88

99
class PasswordConfirmedResponse implements PasswordConfirmedResponseContract
1010
{
@@ -18,6 +18,6 @@ public function toResponse($request)
1818
{
1919
return $request->wantsJson()
2020
? new JsonResponse('', 201)
21-
: redirect()->intended(config('fortify.home'));
21+
: redirect()->intended(Fortify::redirects('password-confirmation'));
2222
}
2323
}

src/Http/Responses/RegisterResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Laravel\Fortify\Http\Responses;
44

55
use Illuminate\Http\JsonResponse;
6-
use Illuminate\Http\Response;
76
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
7+
use Laravel\Fortify\Fortify;
88

99
class RegisterResponse implements RegisterResponseContract
1010
{
@@ -18,6 +18,6 @@ public function toResponse($request)
1818
{
1919
return $request->wantsJson()
2020
? new JsonResponse('', 201)
21-
: redirect()->intended(config('fortify.home'));
21+
: redirect()->intended(Fortify::redirects('register'));
2222
}
2323
}

src/Http/Responses/TwoFactorLoginResponse.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\JsonResponse;
66
use Laravel\Fortify\Contracts\TwoFactorLoginResponse as TwoFactorLoginResponseContract;
7+
use Laravel\Fortify\Fortify;
78

89
class TwoFactorLoginResponse implements TwoFactorLoginResponseContract
910
{
@@ -17,6 +18,6 @@ public function toResponse($request)
1718
{
1819
return $request->wantsJson()
1920
? new JsonResponse('', 204)
20-
: redirect()->intended(config('fortify.home'));
21+
: redirect()->intended(Fortify::redirects('login'));
2122
}
2223
}

0 commit comments

Comments
 (0)