redirect admin to login page if authentication laravel code example
Example 1: how to redirect back to admin page if user is not authenticated in laravel based on the guard
Redirect unauthenticated user according to their guards
Change your Authenticate middleware to this
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Arr;
class Authenticate extends Middleware
{
protected $guards;
public function handle($request, Closure $next, ...$guards)
{
$this->guards = $guards;
return parent::handle($request, $next, ...$guards);
}
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if (Arr::first($this->guards) === 'admin') {
return route('admin.login');
}
return route('login');
}
}
}
Example 2: how to redirect back to admin page if user is not authenticated in laravel based on the guard
Redirect unauthenticated user according to their guards