Skip to content

Instantly share code, notes, and snippets.

@finagin
Created April 20, 2017 14:35
Show Gist options
  • Save finagin/34a989d5e1216f17cd2bd8a52a79c2e2 to your computer and use it in GitHub Desktop.
Save finagin/34a989d5e1216f17cd2bd8a52a79c2e2 to your computer and use it in GitHub Desktop.
Laravel Spark disable registration

Laravel Spark disable registration

php artisan make:command CreateSuperUser

Open app/Console/Commands/CreateSuperUser.php and insert

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Laravel\Spark\Spark;

class CreateSuperUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'super-user:create';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create admin user';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        if (env('SUPERUSER_NAME', false) && env('SUPERUSER_EMAIL', false) && env('SUPERUSER_PASSWORD', false)) {
            $name     = env('SUPERUSER_NAME');
            $email    = env('SUPERUSER_EMAIL');
            $password = env('SUPERUSER_PASSWORD');
        } else {
            $name = env('SUPERUSER_NAME', $this->ask('Name'));

            $email = env('SUPERUSER_EMAIL', $this->ask('E-mail'));

            $password = env('SUPERUSER_PASSWORD', $this->secret('Password'));
            $confirm  = env('SUPERUSER_PASSWORD', $this->secret('Confirm password'));

            if ($password !== $confirm) {
                $this->error('ERROR: Password and confirmation do not match');

                return false;
            }
        }

        if (strlen($password) < 6) {
            $this->error('ERROR: The password at least 6 characters');

            return false;
        }

        Spark::user()
             ->forceFill(
                 [
                     'name'     => $name,
                     'email'    => $email,
                     'password' => bcrypt($password),
                 ]
             )
             ->save();

        $this->info('The superuser is created!');
    }
}

Open app/Console/Kernel.php find $commands array and append our command class

protected $commands = [
    Commands\CreateSuperUser::class
];

routes/web.php

Route::any('/register', function(){
    return redirect()
        ->to('/login', 302)
        ->with('status', 'Registration disabled.');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment