I am developing a mobile app using laravel as backend for APIs but I need some auth pages of the web to be directly accessible from app, therefore I implemented the below middleware to check auth or auth:sanctum
public function handle(Request $request, Closure $next): Response
{
if (Auth::guard('web')->check()) {
// Session-based authentication successful
return $next($request);
} elseif (Auth::guard('sanctum')->check()) {
// Token-based authentication successful
Auth::shouldUse('sanctum');
if (session('id') == null) {
session()->start();
$request->session()->regenerate();
}
return $next($request);
} else {
if ($request->is('api/*') || $request->wantsJson() || $request->hasHeader('Authorization')) {
return response()->json(['message' => 'Unauthenticated.'], 401);
}
// Neither authentication passed
return redirect()->route('login');
}
}
The datatable is failing to load because the ajax loading the datatable doesnot have any auth. How can fix that? I have implemented a session initition logic above however it is not working.