php - Enable register page only after I logged in - Laravel 5.4 -


i'm using make:auth method in laravel 5.4. now, want change behavior of register option. want have register option users logged in admin , other users register option should disabled. original version allows register before being authenticated. want use register option make admin add new users. tried option of authenticating user , redirect register page in home.blade.php doesn't work. learning laravel new project.so expert advice on how proceed.

currently there's method in app\http\controllers\auth\registercontroller

public function __construct() {     $this->middleware('guest'); } 

this making users not logged in can access page. instead, let's change meet needs of 1 of criteria:

public function __construct() {     $this->middleware('auth'); } 

great, must logged in access it. being admin? let's make middleware, now:

php artisan make:middelware adminmiddleware 

now let's open file @ app\http\middleware\adminmiddleware , make adjustments.

note have make assumptions codebase here.

public function handle($request, closure $next) {     if ( auth()->check() && auth()->user()->hasrole('admin')) {         return $next($request);     }      return redirect('/'); } 

next, let's register our application middleware. open app\http\kernel.php , scroll bottom:

protected $routemiddleware = [     'admin' => \app\http\middleware\adminmiddleware::class, // <--add     'auth' => \illuminate\auth\middleware\authenticate::class,     'auth.basic' => \illuminate\auth\middleware\authenticatewithbasicauth::class,     'bindings' => \illuminate\routing\middleware\substitutebindings::class,     'can' => \illuminate\auth\middleware\authorize::class,     'guest' => \app\http\middleware\redirectifauthenticated::class,     'throttle' => \illuminate\routing\middleware\throttlerequests::class, ]; 

finally let's go app\http\controllers\auth\registercontroller , update our construct function utilize middleware:

public function __construct() {     $this->middleware(['auth', 'admin']); } 

now user must logged in, , must have role of admin.

there's surely shorter ways done, method prefer repeating (dry programming).


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -