To customize the redirect behavior after user registration in Laravel Fortify, you can follow these steps:
1. Create a custom `RegisterResponse` class:
- Create a new file called `RegisterResponse.php` in your `app/Http/Responses/Fortify` directory.
- Implement the `RegisterResponseContract` interface and override the `toResponse` method.
- In the `toResponse` method, you can check the user's status and redirect them to the appropriate URL with a custom message.
php
<?php
namespace App\Http\Responses\Fortify;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
class RegisterResponse implements RegisterResponseContract
{
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
$user = $request->user();
if ($user->status === 'active') {
if ($request->wantsJson()) {
return new JsonResponse('', 201);
}
return redirect()->intended(config('fortify.home'));
} elseif ($user->status === 'inactive') {
if ($request->wantsJson()) {
return new JsonResponse(['message' => 'Your account is inactive.'], 403);
}
return redirect()->route('login')->withErrors(['status' => 'Your account is inactive.']);
} elseif ($user->status === 'blocked') {
if ($request->wantsJson()) {
return new JsonResponse(['message' => 'Your account is blocked.'], 403);
}
return redirect()->route('login')->withErrors(['status' => 'Your account is blocked.']);
}
// Default behavior
if ($request->wantsJson()) {
return new JsonResponse('', 201);
}
return redirect()->intended(config('fortify.home'));
}
}
2. Register the custom `RegisterResponse` class:
- Open your `app/Providers/FortifyServiceProvider.php` file.
- In the `boot` method, bind the `RegisterResponseContract` to your custom `RegisterResponse` class.
php
<?php
namespace App\Providers;
use App\Http\Responses\Fortify\RegisterResponse;
use Laravel\Fortify\Contracts\RegisterResponse as RegisterResponseContract;
use Laravel\Fortify\Fortify;
use Illuminate\Support\ServiceProvider;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::loginView(function () {
return view('auth.login');
});
$this->app->bind(RegisterResponseContract::class, RegisterResponse::class);
}
}
In the custom `RegisterResponse` class, we check the user's status and redirect them accordingly. If the user's status is `active`, they are redirected to the `fortify.home` configuration value. If the status is `inactive` or `blocked`, the user is redirected to the login page with an error message.
You can further customize the redirect logic and error messages based on your application's requirements.
Citations:[1] https://laravel.com/docs/11.x/fortify
[2] https://github.com/laravel/fortify/blob/1.x/src/Http/Controllers/RegisteredUserController.php
[3] https://dev.to/snehalkadwe/how-to-override-fortify-default-login-behavior-in-jetstream-gn5
[4] https://github.com/laravel/fortify/issues/77
[5] https://stackoverflow.com/questions/64074079/laravel-fortify-customize-authentication-redirect