To replace Laravel UI authentication with Laravel Fortify and update the existing routes, you can follow these steps:
- Install Laravel Fortify by running the following command in your terminal:
composer require laravel/fortify
- Publish the vendor files by running the following command:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
- Update the
configureRoutesmethod in theapp/Providers/FortifyServiceProvider.phpfile to load the Fortify routes:
protected function configureRoutes()
{
Route::group([
'namespace' => 'Laravel\Fortify\Http\Controllers',
'domain' => config('fortify.domain', null),
'prefix' => config('fortify.prefix'),
], function () {
$this->loadRoutesFrom(base_path('routes/fortify.php'));
});
}
- Now, you need to update the existing routes in
web.phpto work with Laravel Fortify. Here's how you can update them:
use Laravel\Fortify\Http\Controllers\AuthenticatedSessionController;
use Laravel\Fortify\Http\Controllers\RegisteredUserController;
use Laravel\Fortify\Http\Controllers\PasswordResetLinkController;
use Laravel\Fortify\Http\Controllers\NewPasswordController;
use Laravel\Fortify\Http\Controllers\ConfirmablePasswordController;
use Laravel\Fortify\Http\Controllers\TwoFactorAuthenticatedSessionController;
use Laravel\Fortify\Http\Controllers\TwoFactorAuthenticationController;
use Laravel\Fortify\Http\Controllers\ProfileInformationController;
use Laravel\Fortify\Http\Controllers\ProfilePhotoController;
use Laravel\Fortify\Http\Controllers\PasswordController;
use Laravel\Fortify\Http\Controllers\EmailVerificationNotificationController;
use Laravel\Fortify\Http\Controllers\EmailVerificationPromptController;
use Laravel\Fortify\Http\Controllers\VerifyEmailController;
Route::get('login', [AuthenticatedSessionController::class, 'create'])->name('login');
Route::post('login', [AuthenticatedSessionController::class, 'store']);
Route::get('logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');
Route::post('logout', [AuthenticatedSessionController::class, 'destroy'])->name('logout');
Route::post('password/email', [PasswordResetLinkController::class, 'store'])->name('password.email');
Route::get('password/reset', [NewPasswordController::class, 'create'])->name('password.request');
Route::post('password/reset', [NewPasswordController::class, 'store'])->name('password.update');
Route::get('password/reset/{token}', [NewPasswordController::class, 'edit'])->name('password.reset');
Route::get('register', [RegisteredUserController::class, 'create'])->name('register');
Route::post('register', [RegisteredUserController::class, 'store'])->middleware('honeypot');
// Two-factor authentication routes
Route::get('two-factor-challenge', [TwoFactorAuthenticatedSessionController::class, 'create'])->name('two-factor.login');
Route::post('two-factor-challenge', [TwoFactorAuthenticatedSessionController::class, 'store']);
Route::post('user/two-factor-authentication', [TwoFactorAuthenticationController::class, 'store'])->name('two-factor.enable');
Route::delete('user/two-factor-authentication', [TwoFactorAuthenticationController::class, 'destroy'])->name('two-factor.disable');
// Profile routes
Route::put('user/profile-information', [ProfileInformationController::class, 'update'])->name('user-profile-information.update');
Route::put('user/photo', [ProfilePhotoController::class, 'update'])->name('user-profile-photo.update');
Route::put('user/password', [PasswordController::class, 'update'])->name('user-password.update');
// Email verification routes
Route::get('email/verify', [EmailVerificationPromptController::class, '__invoke'])->name('verification.notice');
Route::get('email/verify/{id}/{hash}', [VerifyEmailController::class, '__invoke'])->middleware(['signed', 'throttle:6,1'])->name('verification.verify');
Route::post('email/verification-notification', [EmailVerificationNotificationController::class, 'store'])->middleware(['throttle:6,1'])->name('verification.send');
- Make sure to update the corresponding controller namespaces in the routes.
With these updated routes, you should be able to use Laravel Fortify for authentication, including two-factor authentication.