How to remove the registration feature in Laravel authentication
Laravel comes with authentication out of the box. When you create a new application, the controllers for registration, authentication and resetting passwords are already there. You can then create routes and views for all the authentication features by running an artisan command: php artisan make:auth
.
Lets say you want to use the default authentication feature of Laravel, but you don't want the registration feature. It's an easy thing to do, you only need to make a few modifications:
1. Remove the register links
Before we do anything else, lets remove the register links from our views.
Remove this line from resources/views/welcome.blade.php
:
<a href="{{ route('register') }}">Register</a>
and remove this line from resources/views/layouts/app.blade.php
:
<li><a href="{{ route('register') }}">Register</a></li>
2. Remove the register view
Remove the resources/views/auth/register.blade.php
file.
3. Remove the register controller
Remove the app/Http/Controllers/Auth/RegisterController.php
file.
4. Remove the register routes
This is the most complicated step. By default, the php artisan make:auth
command puts this Auth::routes()
in our routes/web.php
file. If you dig into the framework code, you'll find that this line actually calls the auth()
method of Illuminate/Routing/Router
. It looks like this:
/**
* Register the typical authentication routes for an application.
*
* @return void
*/
public function auth()
{
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
}
So we can just pick the routes that we need. Lets replace the Auth::routes()
line in our routes/web.php
file with this:
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
And that's it!