use Illuminate\Contracts\Auth\Guard; class UserController { protected $guard; public function __construct(Guard $guard) { $this->guard = $guard; } public function index() { $user = $this->guard->user(); if ($user) { return "Welcome, {$user->name}!"; } else { return "Please log in to continue."; } } }
use Illuminate\Contracts\Auth\Guard; class LoginController { protected $guard; public function __construct(Guard $guard) { $this->guard = $guard; } public function login(Request $request) { $credentials = $request->only(['email', 'password']); if ($this->guard->attempt($credentials)) { return redirect()->intended('/'); } else { return redirect('login')->withErrors(['login_failed' => 'Invalid credentials!']); } } }This code attempts to authenticate a user with the provided email and password, and redirects to the home page if successful. The `Guard` interface is part of the Laravel framework's authentication system, which is provided by the `illuminate/auth` package.