I got error in my Laravel project when after i create session and middleware.
middleware:
<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
use IlluminateSupportFacadesLog;
class EnsureAuthenticated
{
public function handle(Request $request, Closure $next)
{
if (!session()->has('auth_token')) {
Log::info('No auth_token in session, redirecting to login.');
return redirect('/login');
}
Log::info('auth_token found, processing request.');
return $next($request);
}
}
web.php codes:
Route::get('/login', function () {
return view('pages.auth.login');
})->name('login');
Route::post('/login', function (Request $request) {
$validatedData = $request->validate([
'email' => 'required|email',
'password' => 'required'
]);
$response = Http::post('https://api.gsutil.xyz/admin/login', [
'email' => $validatedData['email'],
'password' => $validatedData['password'],
]);
if ($response->successful()) {
$apiResponse = $response->json();
session(['email' => $validatedData['email']]); // Save the email in the session
return response()->json([
'message' => $apiResponse['message'] ?? 'Logged in successfully',
'success' => true,
]);
} else {
return response()->json([
'message' => 'Invalid credentials',
'success' => false,
], 401);
}
});
Route::middleware(['web'])->group(function () {
Route::get('/', function () {
return view('pages.home');
});
// Product Routes
Route::get('/product', [ProductController::class, 'showData'])->name('product.manage');
Route::delete('/product/{id}', [ProductController::class, 'deleteProduct'])->name('product.delete');
Route::get('/add-product', [ProductController::class, 'addProductForm'])->name('product.add');
Route::post('/add-product', [ProductController::class, 'submitProduct'])->name('product.submit');
});
bootstrap > app.php
<?php
use IlluminateFoundationApplication;
use IlluminateFoundationConfigurationExceptions;
use IlluminateFoundationConfigurationMiddleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__ . '/../routes/web.php',
commands: __DIR__ . '/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->appendToGroup('web', [
AppHttpMiddlewareEnsureAuthenticated::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();
these are the my files. first few minutes code work properly. but unfortunately my browser show:
This page isn’t working
127.0.0.1 redirected you too many times.
Try deleting your cookies.
ERR_TOO_MANY_REDIRECTS
i tried clean my browser cookies but it does not work. How can i fix this. I got these error in my console:
helpers.php vendorlaravelframeworksrcILLuminateFoundation 11
[{
"resource": "/c:/Users/user/Desktop/Laravel/order-processing-backend/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php",
"owner": "_generated_diagnostic_collection_name_#1",
"code": "PHP0443",
"severity": 4,
"message": "Too many arguments to function get(). 2 provided, but 1 accepted.",
"source": "PHP",
"startLineNumber": 276,
"startColumn": 41,
"endLineNumber": 276,
"endColumn": 49
},..............
}]
and also my laravel log file repeat same record many times when load routes.
[2024-05-10 21:05:06] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:06] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:06] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:06] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:07] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:07] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:07] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:07] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:07] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:07] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:07] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:08] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:08] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:08] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:08] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:08] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:08] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:08] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:09] local.INFO: No auth_token in session, redirecting to login.
[2024-05-10 21:05:09] local.INFO: No auth_token in session, redirecting to login.
how to fix this issue. please help me to fix this.
i want solution to fix this error: 127.0.0.1 redirected you too many times. Try deleting your cookies. ERR_TOO_MANY_REDIRECTS