401 Unauthorized : A security token is required but the token storage is empty. SYMFONY6

i’m working with symfony 6 and react js , im trying to access the cart of the user and it works correctly when I test it with postman but it throws this error when I test it in my frontend interface
401 Unauthorized : A security token is required but the token storage is empty.

this is my api function :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class PanierController extends AbstractController
public function __construct(TokenStorageInterface $tokenStorage) {
$this->tokenStorage = $tokenStorage;
}
#[Route('/api/get_panier', name: 'get-panier', methods: ['GET','POST'])]
public function getPanierContents( TokenInterface $token , Request $request ): Response
{
$token = $this->tokenStorage->getToken();
if (!$token) {
return $this->json(['message' => 'Token not found'], Response::HTTP_UNAUTHORIZED);
}
$user = $token->getUser();
if (!$user || !$user instanceof UserInterface) {
$this->logger->error("User not found");
return $this->json(['message' => 'User not found'], Response::HTTP_NOT_FOUND);
}
error_log("User found: " . $user->getUserIdentifier());
$panier = $user->getPanier();
if (!$panier) {
return $this->json(['message' => 'Panier not found'], Response::HTTP_NOT_FOUND);
}
$billets = $panier->getBillets();
// Convert billets collection to array
$billetsArray = [];
foreach ($billets as $billet) {
$billetsArray[] = [
'id' => $billet->getId(),
'date_debut_evenementt'=>$billet->getDateDebutEvenement(),
// Add other properties you want to include
];
}
// $serializedContents = $serializer->serialize($contents, 'json', ['groups' => 'billet']);
return $this->json($billetsArray);
}
</code>
<code>class PanierController extends AbstractController public function __construct(TokenStorageInterface $tokenStorage) { $this->tokenStorage = $tokenStorage; } #[Route('/api/get_panier', name: 'get-panier', methods: ['GET','POST'])] public function getPanierContents( TokenInterface $token , Request $request ): Response { $token = $this->tokenStorage->getToken(); if (!$token) { return $this->json(['message' => 'Token not found'], Response::HTTP_UNAUTHORIZED); } $user = $token->getUser(); if (!$user || !$user instanceof UserInterface) { $this->logger->error("User not found"); return $this->json(['message' => 'User not found'], Response::HTTP_NOT_FOUND); } error_log("User found: " . $user->getUserIdentifier()); $panier = $user->getPanier(); if (!$panier) { return $this->json(['message' => 'Panier not found'], Response::HTTP_NOT_FOUND); } $billets = $panier->getBillets(); // Convert billets collection to array $billetsArray = []; foreach ($billets as $billet) { $billetsArray[] = [ 'id' => $billet->getId(), 'date_debut_evenementt'=>$billet->getDateDebutEvenement(), // Add other properties you want to include ]; } // $serializedContents = $serializer->serialize($contents, 'json', ['groups' => 'billet']); return $this->json($billetsArray); } </code>
class PanierController extends AbstractController

    public function __construct(TokenStorageInterface $tokenStorage) {
        $this->tokenStorage = $tokenStorage;
    }

  #[Route('/api/get_panier', name: 'get-panier', methods: ['GET','POST'])]

    public function getPanierContents(  TokenInterface $token , Request $request ): Response
    {
        $token = $this->tokenStorage->getToken();
        if (!$token) {
            return $this->json(['message' => 'Token not found'], Response::HTTP_UNAUTHORIZED);
        }

        $user = $token->getUser();
        if (!$user || !$user instanceof UserInterface) {
            $this->logger->error("User not found");
            return $this->json(['message' => 'User not found'], Response::HTTP_NOT_FOUND);
        }
  error_log("User found: " . $user->getUserIdentifier());

        $panier = $user->getPanier();

        if (!$panier) {
            return $this->json(['message' => 'Panier not found'], Response::HTTP_NOT_FOUND);
        }

        $billets = $panier->getBillets();

        // Convert billets collection to array
        $billetsArray = [];
        foreach ($billets as $billet) {
            $billetsArray[] = [
                'id' => $billet->getId(),
                'date_debut_evenementt'=>$billet->getDateDebutEvenement(),
                // Add other properties you want to include
            ];
        }
    //    $serializedContents = $serializer->serialize($contents, 'json', ['groups' => 'billet']);

        return $this->json($billetsArray);
    

}
  

and this is my security.yaml config :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> app_user_provider:
entity:
class: AppEntityUsers
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
custom_authenticator: AppSecurityUsersAuthenticator
logout:
path: app_logout
target: connexion_page
login:
stateless: true
json_login:
check_path: /api/login
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
stateless: true
jwt: ~
</code>
<code> app_user_provider: entity: class: AppEntityUsers property: email firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: lazy: true provider: app_user_provider custom_authenticator: AppSecurityUsersAuthenticator logout: path: app_logout target: connexion_page login: stateless: true json_login: check_path: /api/login username_path: email password_path: password success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure api: pattern: ^/api stateless: true jwt: ~ </code>
    app_user_provider:
      entity:
        class: AppEntityUsers
        property: email

  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    main:
      lazy: true
      provider: app_user_provider
      custom_authenticator: AppSecurityUsersAuthenticator
     
      logout:
        path: app_logout
        target: connexion_page
      
    login:
        stateless: true
        json_login:
            check_path: /api/login
            username_path: email
            password_path: password
            success_handler: lexik_jwt_authentication.handler.authentication_success
            failure_handler: lexik_jwt_authentication.handler.authentication_failure
      
    api:
        pattern:   ^/api
        stateless: true
        jwt: ~

and this is how i am consuming it in react js :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export const fetchPanierData = (userId) => {
return async (dispatch) => {
try {
const token = getToken();
if (!token) {
throw new Error("No token found");
}
const response = await axios.get(`${BASE_URL}/api/get_panier`, {
headers: {
'Authorization': `Bearer ${token}`,
},
});
dispatch({ type: "FETCH_PANIER_SUCCESS", payload: response.data });
} catch (error) {
dispatch({
type: "FETCH_PANIER_FAILURE",
payload: error.response ? error.response.data : error.message,
});
}
};
};
</code>
<code>export const fetchPanierData = (userId) => { return async (dispatch) => { try { const token = getToken(); if (!token) { throw new Error("No token found"); } const response = await axios.get(`${BASE_URL}/api/get_panier`, { headers: { 'Authorization': `Bearer ${token}`, }, }); dispatch({ type: "FETCH_PANIER_SUCCESS", payload: response.data }); } catch (error) { dispatch({ type: "FETCH_PANIER_FAILURE", payload: error.response ? error.response.data : error.message, }); } }; }; </code>
export const fetchPanierData = (userId) => {
  return async (dispatch) => {
    try {
      const token = getToken();
      if (!token) {
        throw new Error("No token found");
      }
      const response = await axios.get(`${BASE_URL}/api/get_panier`, {
        headers: {
          'Authorization': `Bearer ${token}`,
          
        },
      });
      dispatch({ type: "FETCH_PANIER_SUCCESS", payload: response.data });
    } catch (error) {
      dispatch({
        type: "FETCH_PANIER_FAILURE",
        payload: error.response ? error.response.data : error.message,
      });
    }
  };
};

I think the problem is in my security.yaml config so if you can help me that be great

before using TokenInterface $token in my api function I tried with #[currentUser] but it gave me a similar problem where it worked fine in postman but kept throwing a user not found error in my frontend

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật