How to disable registration in laravel 5 and above

Laravel 5 comes with build-in login and registration functionality.  You can easily enable it, but the hard part is to disable registration, because by default anyone can register an account and login to your admin panel. So, it is important to disable registration.

To enable build-in login feature, follow this:
How to enable Authentication in laravel 5 and above.

Their are many different solution to disable registration in laravel 5. Some of them are:

1. Set Registration for Authenticated user only (recommend)


This method doesn't disable registration but enable registration only for authenticated users; that is, a user must be logged-in to create new account.

Step 1: Go into /app/http/controllers/auth
Step 2: Open RegisterController.php into any editor.
Step 3. Now, Find __construct() function that is
       public function __construct(){
$this->middleware(‘guest’);
       }
Step 4: Change $this->middleware(‘guest’) to $this->middleware(‘auth’)

 $this->middleware(‘guest’)  means, registration is available for all users.
 $this->middleware(‘auth’) means, registration is available for authenticated users only.

Now, whenever the non-logged-in user tries to open registration page, it will get redirected to login page.

2. Delete the registration route to disable regisatration in laravel

This is most simple approach. Just go to your route.php file and delete registration route.


3. Make redirection to login to disable registration in laravel

This will disable the registration. Whenever any user tries to open registration page, it will redirect to login page. For this:

Step 1: Go into /app/http/controllers/auth
Step 2: Open RegisterController.php into any editor.
Step 3: Override the showRegistrationForm() with the following code.
protected function showRegistrationForm() {     return redirect()->to('login'); }
Now, when the url /register is evoked, it will redirect to the login page and display a warning message. of course, you can redirect it to where ever you want; just change 'login' to specified url.

So, these are 3 different methods to disable registration in laravel 5 and above. For any problem, leave a comment, and bunch of us will try to help you.


Comments