1. Ensure Guard is Defined in auth.php
In config/auth.php, make sure the college guard is properly configured:
'guards' => [
'college' => [
'driver' => 'session',
'provider' => 'colleges', // Must match the provider below
],
],
'providers' => [
'colleges' => [
'driver' => 'eloquent',
'model' => App\Models\College::class, // Update with your College model
],
],
2. Log In User with Guard
To manually authenticate a user under the college guard, use:
use Illuminate\Support\Facades\Auth;
use App\Models\College;
$user = College::find(1); // Replace 1 with the user ID you want to assign
if ($user) {
Auth::guard('college')->login($user);
}
3. Retrieve Logged-in User
Once authenticated, you can retrieve the user ID using:
$userId = Auth::guard(‘college’)->user()->id;
4. Logout the User
To log out the user from the college guard:
Auth::guard(‘college’)->logout();
5. Middleware for Protecting Routes (Optional)
If you want to protect routes so that only college users can access them, define middleware in routes/web.php:
Route::middleware(['auth:college'])->group(function () {
Route::get('/college-dashboard', function () {
return "Welcome, College User!";
});
});
Other Articles
How to check Auth in app service provider in laravel
