Level 1
hello there ^^. have you solved it . i have the exact same problem $$
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I'm trying to set up a Laravel backend that interacts with a React-Native frontend application using Sanctum.
I've set up a simple test route in routes/api.php but whenever I make a request to it I get the following error:
local.ERROR: Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '512' frames {"exception":"[object] (Error(code: 0): Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '512' frames at
C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:1339)
[stacktrace]
#0 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(968): Illuminate\Foundation\Application->isDeferredService('config')
#1 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(955): Illuminate\Foundation\Application->loadDeferredProviderIfNeeded('config')
#2 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Container\Container.php(731): Illuminate\Foundation\Application->resolve('config', Array)
#3 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(942): Illuminate\Container\Container->make('config', Array)
#4 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(120): Illuminate\Foundation\Application->make('config', Array)
#5 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(274): app('config')
#6 C:\Users\George\Projects\gig-app\backend\vendor\laravel\sanctum\src\Guard.php(56): config('sanctum.guard', 'web')
#7 [internal function]: Laravel\Sanctum\Guard->__invoke(Object(Illuminate\Http\Request), Object(Illuminate\Auth\EloquentUserProvider))
#8 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Auth\RequestGuard.php(57): call_user_func(Object(Laravel\Sanctum\Guard), Object(Illuminate\Http\Request), Object(Illuminate\Auth\EloquentUserProvider))
#9 C:\Users\George\Projects\gig-app\backend\vendor\laravel\sanctum\src\Guard.php(57): Illuminate\Auth\RequestGuard->user()
#10 [internal function]: Laravel\Sanctum\Guard->__invoke(Object(Illuminate\Http\Request), Object(Illuminate\Auth\EloquentUserProvider))
#11 C:\Users\George\Projects\gig-app\backend\vendor\laravel\framework\src\Illuminate\Auth\RequestGuard.php(57): call_user_func(Object(Laravel\Sanctum\Guard), Object(Illuminate\Http\Request), Object(Illuminate\Auth\EloquentUserProvider))
The route is:
Route::middleware('auth:sanctum')->get('/test', function (Request $request) {
return response()->json(['message' => 'Welcome to Laravel'], 200);
});
I've installed and configured Sanctum as follows in config/auth.php:
return [
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_reset_tokens',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
And in Kernel.php I've uncommented EnsureFrontendRequestsAreStateful:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
And this is App.js in the React-Native app:
import React, { useEffect, useState } from "react";
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View } from "react-native";
export default function App() {
const [message, setMessage] = useState("");
useEffect(() => {
fetch("https://ngrok-free.app/api/test", {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
.then((response) => response.json())
.then((data) => setMessage(data.message))
.catch((error) => console. Error(error));
}, []);
return (
<View style={styles.container}>
<Text>The message from the server is:</Text>
<Text>{message}</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
Please or to participate in this conversation.