Search code examples
symfonyshopwareshopware6

Shopware 6 Authorize customers


I would like to create a controller for providing downloadable files to a specific customer group. I need to authenticate the customer and check if they are authorized.

I checked the context and the HTTP header but I was unable to find valuable data about the customer.

What would be an appropriate approach?


Solution

  • To restrict access to your own route to only logged in customers you can use the @LoginRequired annotations, e.g.:

        /**
         * @LoginRequired()
         * @Route("/account", name="frontend.account.home.page", methods={"GET"})
         */
        public function index(Request $request, SalesChannelContext $context, CustomerEntity $customer): Response
    

    Under the hood the annotation will also check the current customer of the SalesChannelContext.

    To get the customer group of the current customer, your proposed solution is the way to go:

    $salesChannelContext->getCustomer()->getGroup();