function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jose CunhaJose Cunha 

how to get Guest user's IP address within @auraenabled methods?

Hi all,

I'm wondering if there's any way to obtain Guest User's IP Address within an @AuraEnabled method called by a LWC sitting on a Community.

I tried different approaches and they all failed short, let me please tell you why:

- ApexPages.currentPage().getHeaders(): It has only to do with VisualForce pages, which is not the case.

- Auth.SessionManagement.getCurrentSession(): It raises exception due to the fact that Guest users do not have sessions.

- Any sort of JS only solutions (e.g. https://api.ipify.org/): The idea behind this solution is to make a request to an external server, which is responsible to return client's ip. Then call any @AuraEnabled method to inform what IP was. Well, this seems too brittle to me. First because it introduces another point of failure (let's assume the external server is down - difficult but not impossible), second because it's to easy to dodge the second request with any arbitrary IP.

Is there any other solution I might be missing?

Thanks,
Jose
Nayana KNayana K
http://www.sfdcdrona.com/2019/09/retrieve-ip-address-for-guest-user-in.html?m=1

Did you get a chance to try this alternative?
Krysia WojtynaKrysia Wojtyna
Nayana k, I did try it.
I doesn't work for obtaining the IP of the community guest user in the AuraEnabled Apex code.

It works only when the VisualPage is placed directly in the community site - in such case it produces the correct (user) IP.
Unfortunately when it's called from the Apex controller, exactly as advised - the captured IP is the IP of the Salesforce server