To customize the API response in Laravel Fortify, you can follow these steps:
1. Create a custom `LoginResponse` class:
- Create a new file called `LoginResponse.php` in your `app/Http/Responses/Fortify` directory.
- Implement the `LoginResponseContract` interface and override the `toResponse` method.
- In the `toResponse` method, you can customize the API response for successful and failed login attempts.
php
<?php
namespace App\Http\Responses\Fortify;
use Illuminate\Http\JsonResponse;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
use Laravel\Fortify\Fortify;
class LoginResponse implements LoginResponseContract
{
/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
// Check if the login was successful
if ($request->user()) {
return new JsonResponse([
'message' => 'Login successful',
'user' => $request->user()->only('id', 'name', 'email'),
'token' => $request->user()->createToken(Fortify::$guard)->plainTextToken,
]);
} else {
return new JsonResponse([
'message' => 'Invalid credentials',
], 401);
}
}
}
2. Register the custom `LoginResponse` class:
- Open your `app/Providers/FortifyServiceProvider.php` file.
- In the `boot` method, bind the `LoginResponseContract` to your custom `LoginResponse` class.
php
<?php
namespace App\Providers;
use App\Http\Responses\Fortify\LoginResponse;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;
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(LoginResponseContract::class, LoginResponse::class);
}
}
In the custom `LoginResponse` class, we check if the login was successful and return a JSON response with the appropriate data. If the login fails, we return a 401 Unauthorized response with an error message.
You can further customize the response structure and include additional information as per your application's requirements.
Similarly, you can create custom response classes for other Fortify actions, such as registration, password reset, and email verification, by following the same approach.
Citations:[1] https://wpwebinfotech.com/blog/laravel-fortify/
[2] https://laracasts.com/discuss/channels/laravel/how-do-i-customize-the-laravel-8-fortifysanctum-json-response-on-login-and-register-with-an-api-resource
[3] https://www.youtube.com/watch?v=NVv-_GxOEuU
[4] https://laravel.com/docs/11.x/fortify
[5] https://github.com/laravel/fortify/blob/1.x/src/Http/Controllers/RegisteredUserController.php