Disable Auth Register route in Laravel 8?

Viewed 16647

I need to disable my register route in Laravel 8. Tried

Auth::routes([
     'register' => false,
     'login' => false,
]);

but the application threw up an error.

RuntimeException
In order to use the Auth::routes() method, please install the laravel/ui package.

If anyone points out what needs to change, will be grateful.

Thanks

8 Answers

Laravel 8 uses fortify authentication. To disable registration from your laravel app, you need to disable it from fortify, which is located on /config/fortify.php

'features' => [
    // Features::registration(), // disable here
     Features::resetPasswords(),
     Features::emailVerification(),
     Features::updateProfileInformation(),
     Features::updatePasswords(),
     Features::twoFactorAuthentication(),
],

At the end of my routes/web.php was the following line:

require __DIR__.'/auth.php';

In routes/auth.php are listed all additional routes for user login/register/logout. Just comment out or remove /register route from there.

In addition, be sure to disable related routes, in routes/web.php :

Route::get('/register', function() {
    return redirect('/login');
});

Route::post('/register', function() {
    return redirect('/login');
});

I changed according feature tests in tests/Feature/RegistrationTest.php to try to keep work clean so I needed those redirections.

Remove the registration routes from config/auth.php and then create a config/fortify.php (paste the content from: vendor/laravel/fortify/config/fortify.php) which will override the default settings.

Inside config/fortify.php comment out the first element of features array (Features::registration()) then run php artisan optimize to clear config cache and routes cache.

Now all your removed routes should return 404, you can also check if those still exist with php artisan route:list

config/fortify.php:

<?php

use Laravel\Fortify\Features;

return [
    'guard' => 'web',
    'middleware' => ['web'],
    'passwords' => 'users',
    'username' => 'email',
    'email' => 'email',
    'views' => true,
    'home' => '/home',
    'prefix' => '',
    'domain' => null,
    'limiters' => [
        'login' => null,
    ],
    'features' => [
        //Features::registration(),
        Features::resetPasswords(),
        Features::emailVerification(),
        Features::updateProfileInformation(),
        Features::updatePasswords(),
        Features::twoFactorAuthentication(),
    ],
];

Just use:

Auth::routes(['register' => false]);

Do not break your head with different versions of packages and Laravel. Because maybe you don't have fortify.php in your config, or using different packages. All routes are in routes/web now. Just go there and force that '/register' sends to login or any other view you want to:

Route::any('/register', function() {
    return  view('auth.login');
});

That way you maintain out of reach that feature, but close for when you need it.

Remove this code from routes/auth.php

Route::get('/register', [RegisteredUserController::class, 'create'])
                ->middleware('guest')
                ->name('register');

Route::post('/register', [RegisteredUserController::class, 'store'])
                ->middleware('guest');

Just put this in your /routes/web.php:

Route::any('/register', [App\Http\Controllers\HomeController::class, 'index']);
Related