Disable registration in built-in registration Laravel 5.3
I use built-in authentication in laravel 5.3 , but I want disable user registration. What is the best solution for this?
Laravel version 5.3.9 have not AuthController
Maybe if you in this file
https://github.com/laravel/laravel/blob/master/app/Http/Controllers/Auth/RegisterController.php
you remove trait RegistersUsers
// use RegistersUsers;
Method App\Http\Controllers\Auth\RegisterController::showRegistrationForm() does not exist
I disable Auth::routes()
and added
// Login Routes...
Route::get('login', ['as' => 'login', 'uses' => 'Auth\LoginController@showLoginForm']);
Route::post('login', ['as' => 'login.post', 'uses' => 'Auth\LoginController@login']);
Route::post('logout', ['as' => 'logout', 'uses' => 'Auth\LoginController@logout']);
// Registration Routes...
// Route::get('register', ['as' => 'register', 'uses' => 'Auth\RegisterController@showRegistrationForm']);
// Route::post('register', ['as' => 'register.post', 'uses' => 'Auth\RegisterController@register']);
// Password Reset Routes...
Route::get('password/reset', ['as' => 'password.reset', 'uses' => 'Auth\ForgotPasswordController@showLinkRequestForm']);
Route::post('password/email', ['as' => 'password.email', 'uses' => 'Auth\ForgotPasswordController@sendResetLinkEmail']);
Route::get('password/reset/{token}', ['as' => 'password.reset.token', 'uses' => 'Auth\ResetPasswordController@showResetForm']);
Route::post('password/reset', ['as' => 'password.reset.post', 'uses' => 'Auth\ResetPasswordController@reset']);
});
@m.donicova in your Auth\RegisterController.php file override the showRegistrationForm() with the following code.
protected function showRegistrationForm()
{
return redirect()->to('login')->with('warning', 'Registration is disabled.');
}
now when the url /register is envoked it will redirect to the login page and display a warning message. of course, you can redirect it to where ever you want and change the message to whatever you want. this should work on 5.3 and 5.4.
I did this by changing the constructor in Auth\RegisterController.php From:
public function __construct()
{
$this->middleware('guest');
}
to:
public function __construct()
{
$this->middleware('auth');
}
This way you can create new users if you want, but you have to be logged in first.
@erellsworth Fantastic simple solution! Thank you!
@erellsworth Great solution!
@erellsworth 's answer is so good, it should be in the docs!
There is an easier way ... try this: https://blackbits.io/blog/how-to-disable-user-registration-in-laravel
Please or to participate in this conversation.