Hi,
I don't fully understand your question, but let me try to explain how this will work. In the Sling model, you can obtain the user who has started the session that reaches your Sling model. In other words, you can determine which user has logged into AEM, and thus, retrieve its details, such as the group[1]. If you use this same Sling model in the publish instance, you will have to "enable" AEM login publicly for this to work properly.
By default, in the publish instance, the anonymous user is the session used if no other user has started a session. That being said, if you need that Sling Model to work in publish, you will have to log into AEM. This scenario is how the WKND site works. You can refer to that if you need to mimic that functionality. If, on the other hand, you have a custom login, then you will have to rely on other methods to capture that user's information in the Sling model.
Hope this clarifies your doubts.
[1]. How to get the group in slingModel: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-fetch-users-of-a-group-through-java-code/m-p/168443
[2]. Wkdn example: https://experienceleague.adobe.com/docs/experience-manager-learn/getting-started-wknd-tutorial-develop/overview.html