Certainly! Here’s a step-by-step solution for using Laravel Passport for authentication with Blade views, even if your users table does not have a password column (such as when using social authentication or some custom auth):
1. Use Laravel’s session-based authentication for Blade views
Passport is designed for API authentication (typically for SPAs/mobile/etc.), but Laravel Blade views are most easily protected via the built-in session (web guard) authentication. If your users don’t have passwords, you can still manually log them in for Blade-protected views.
2. Add a login route or controller
If you don’t have passwords, you’ll need an alternative authentication method (socialite, magic links, one-time codes, etc.). Let’s say you want to log in an admin by their email:
// routes/web.php
Route::post('/admin-login', function (Illuminate\Http\Request $request) {
$user = \App\Models\User::where('email', $request->email)->first();
if (! $user) {
return back()->withErrors(['email' => 'No such user']);
}
Auth::login($user);
return redirect('/admin');
});
3. Protect your blade views using middleware
// routes/web.php
Route::get('/admin', function () {
return view('admin.dashboard');
})->middleware('auth');
4. If you use Passport for API access only
Passport is for API token authentication and not usually used for authenticating Blade views. If an admin needs to access both API and Blade views, keep the API requests using Passport tokens, and use session (web) guard for logging into the Blade-admin area.
5. Summary
- Use session-based (
webguard) authentication for Blade views. - Use Passport (
apiguard) authentication for APIs. - Since you have no passwords, log in your users using another unique identifier (like email) manually, or use a package like Laravel Socialite.
Example: Socialite for Blade authentication
// routes/web.php
Route::get('login/{provider}', [SocialController::class, 'redirect']);
Route::get('login/{provider}/callback', [SocialController::class, 'callback']);
// app/Http/Controllers/SocialController.php
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use App\Models\User;
class SocialController extends Controller
{
public function redirect($provider)
{
return Socialite::driver($provider)->redirect();
}
public function callback($provider)
{
$socialUser = Socialite::driver($provider)->user();
$user = User::firstOrCreate([
'email' => $socialUser->getEmail(),
]);
Auth::login($user);
return redirect('/admin');
}
}
In short:
Use session (web) authentication for accessing/administering Blade views, and keep Passport for APIs. If you lack passwords, use a different login mechanism (social, magic links, etc.), then manually authenticate users in your login flow for Blade.
Let me know if you need a specific example tailored to your auth flow!