Laravel Sanctum for REST API is not working

319 views Asked by At

I got stuck in the following problem for a long day. I am new in laravel. I am using laravel/sanctum 2.15 for my authentication system. But my sanctum middleware gives the following error:

{
    "message": "Maximum call stack size of 8355840 bytes reached. Infinite recursion?",
    "exception": "Error",
    "file": "/Users/lvbandit/Documents/cuet-alumni/alumni_backend/vendor/laravel/sanctum/src/Guard.php",
    "line": 54,
    "trace": [
        {
            "function": "__invoke",
            "class": "Laravel\\Sanctum\\Guard",
            "type": "->"
        },
        {
            "file": "/Users/lvbandit/Documents/cuet-alumni/alumni_backend/vendor/laravel/framework/src/Illuminate/Auth/RequestGuard.php",
            "line": 57,
            "function": "call_user_func"
        },

I am trying like the following:

Route::group(['middleware' => ['auth:sanctum']],(function() {
    Route::post('logout', 'App\Http\Controllers\Api\V1\AuthController@logout');
    Route::get('user', 'App\Http\Controllers\Api\V1\AuthController@user');
    Route::get('test',function(){
        return response()->json([
            'result' => true,
            'message' => 'test'
        ], 200);
    });
}));

I want to use these endpoints for my react frontend. but its not working on postman let alone react. Please help someone!

I have tried to watch every video on youtube. I have tried in everyways. May be the problem relies in my project as i have tried different project on authentication only that works perfectly. I am using sanctum for both my web.php and app.php. My web.php works perfectly

1

There are 1 answers

0
Sammar malik On

You can use laravel/breeze for api with built-in Sanctum implementation. It also provides default authentication features like login, registration, reset password, & email verification.

https://laravel.com/docs/10.x/starter-kits#laravel-breeze