Disable signup in Laravel's built-in Authentication

In this post I'm using Laravel 5.3 (which is currently the latest one).

Laravel 5.3 is shipped with a built-in authentication system out of the box. It's really easy to setup a project that requires login/register functionality with few artisan commands a puff you're good to go.

But sometimes you might need to disable registration, but keep sign in open.

Let's take a look how setup is done for authenticating users in Laravel. First you need to execute:

php artisan make:auth

This will automatically generate views and routes that are needed. Additionally, a controller HomeController will be placed in your app. This controller is used as an endpoint to your app for authenticated users only.

When you install Laravel, you have two controllers already placed within your App/Http/Controllers directory. One of them is called AuthController which handles the logic for signin up/signing in users. The other one, called PasswordController is used for resetting passwords of already existing users.

We will leave the PasswordController alone, because it is not within the scope of this post.

Having setup the logic behind register and login, take a look at the AuthController. All the magic in this controller is done by the AuthenticatesAndRegistersUsers trait.

You should NEVER EVER edit the code that is in your vendor/ folder!

Now open the trait mentioned above. You should see this:

namespace Illuminate\Foundation\Auth;
trait AuthenticatesAndRegistersUsers
    use AuthenticatesUsers, RegistersUsers {
        AuthenticatesUsers::redirectPath insteadof RegistersUsers;
        AuthenticatesUsers::getGuard insteadof RegistersUsers;

So basically this trait wraps two other traits. To disable registering just go back to your AuthController, remove AuthenticatesAndRegistersUsers and add AuthenticatesUsers.

The last thing you have to do is to override showRegisterForm() and getRegister(), otherwise if users try to open the page with the signup form it'll throw an exception.

Add something like app()->abort(404);. That's all up to you.

unsplash-logoNora Görlitz provided this awesome picture!

Show Comments