birdietorerik's avatar

Route::middleware('auth:sanctum') problem

Hi! Making a Laravel application using Laravel 12.x and Vue3 Have big problem with -> Route::middleware('auth:sanctum')

api.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\ChurchController;
use Illuminate\Support\Facades\Auth;
use Laravel\Sanctum\PersonalAccessToken;
use App\Models\User;
use Illuminate\Http\Request;


Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);

Route::middleware('auth:sanctum')->apiResource('churches', ChurchController::class);

Route::middleware('auth:sanctum')->get('/me', function () {
    return response()->json([
        'user' => auth()->user()
    ]);
});

Route::get('/ping', function () {
    return ['status' => 'API works'];
});

Dashboard.vue

Login.vue

Problem:

After Login, it call Dashboard

But when /api/me is executed the server shut down

Get this error in console


Login.vue:37 [Vue warn]: Unhandled error during execution of mounted hook 
  at <Dashboard onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< 
Proxy(Object) {__v_skip: true}
 > > 
  at <RouterView> 
  at <App>

Dashboard.vue:33 
 GET http://127.0.0.1:8000/api/me net::ERR_EMPTY_RESPONSE
favicon.ico:1 
 GET http://127.0.0.1:8000/favicon.ico net::ERR_CONNECTION_RESET
axios.js?v=bbf5963d:1669 Uncaught (in promise) AxiosError: Network Error
    at async newFunction (Dashboard.vue:33:16)
    at async Dashboard.vue:29:17

Please help

1 like
2 replies
quintinmorrow's avatar

Auth middleware issues with Sanctum can be tricky because a small config mismatch can block the whole route. I’ve run into problems where the guard or API stateful domains weren’t set correctly, so the middleware never recognized the session. Checking the Sanctum config and how the route group is defined usually reveals what’s going wrong.

1 like

Please or to participate in this conversation.