Authentication in Laravel

Asked

Viewed 285 times

0

Using Laravel, what can I do so that only authenticated users can register new users? With this, the user registration page would only be accessed by authenticated users. In Laravel’s default settings the registration is done freely.

1 answer

1


I’ll assume you’re using the version 5.3 Laravel. The simplest way is to assign middleware Auth in the builder of his class \App\Http\Controllers\Auth\RegisterController, thus:

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('auth'); // anteriormente definida como guest.
}

If you check your route list via the command php artisan route:list, will verify that from now on the registration routes (the last two of the following image) are protected by the authentic users middleware (auth).

Rotas de um projeto novo com Laravel versão 5.3. Figure 1: Log routes protected by authentic users' middleware (auth).

If you are using a previous version of Laravel, as this is a very low level question, you can check out the official documentation page. For example, in the controller page version 5.2, there is a specific topic for assigning multiple middleware on routes and in a controller constructor.

  • 1

    Thank you, Rafael! You saved me. rsrs' +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.