Currently to add authentication I use this in my routes file:
Route::middleware(['auth'])->group(function () {
});
But I want to also check for different routes if the user is an admin, so currently I do this and have a custom middleware file:
Route::middleware(['auth', 'admin'])->group(function () {
});
//
<?php
namespace App\Http\Middleware;
use Closure;
class Admin {
public function handle($request, Closure $next)
{
if ( Auth::check() && Auth::user()->isAdmin() )
{
return $next($request);
}
return redirect('dashboard');
}
}
This all works fine, but I noticed with the api middleware it uses this:
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Questions
- How can I make a namespaced auth middleware like the api one
'auth:admin' - Where is the file located that sets the
'auth:api'middleware, I could not find it anywhere in the app folder - Is there any other ways to do multiple auth like editing the file
config/auth.phpand then separating the users between two tables, one for admins and one for other users.