1php artisan make:middleware BasicAuth //In console.
2
3//BasicAuth.php file is created:
4<?php
5namespace App\Http\Middleware;
6
7use Closure;
8use Illuminate\Support\Facades\Auth;
9
10class AdminAuth {
11 /**
12 * Handle an incoming request.
13 *
14 * @param \Illuminate\Http\Request $request
15 * @param \Closure $next
16 * @return mixed
17 */
18 public function handle($request, Closure $next) {
19 return $next($request);
20 }
21}
22
23//Replace handle function:
24public function handle($request, Closure $next) {
25 //The following line(s) will be specific to your project, and depend on whatever you need as an authentication.
26 $isAuthenticatedAdmin = (Auth::check() && Auth::user()->admin == 1);
27
28 //This will be excecuted if the new authentication fails.
29 if (! $isAuthenticatedAdmin)
30 return redirect('/login')->with('message', 'Authentication Error.');
31 return $next($request);
32}
33
34//In app/Http/Kernel.php, add this line:
35protected $routeMiddleware = [
36 /*
37 * All the laravel-defined authentication methods
38 */
39 'adminAuth' => \App\Http\Middleware\AdminAuth::class //Registering New Middleware
40];
41
42//In routes/web.php, add this at the end of the desired routes:
43Route::get('/adminsite', function () {
44 return view('adminsite');
45})->middleware('adminAuth'); //This line makes the route use your new authentication middleware.