Is there an elegant, non-hack way to get the user's IP address from within an LWC component? (Not an Aura component).
Thanks!
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityIs there an elegant, non-hack way to get the user's IP address from within an LWC component? (Not an Aura component).
Thanks!
Assuming that you want to get the external public IP address and not the local one, the basic way would be to adapt the code from here
exampleIP.html
<template>
{ip.data}
</template>
exampleIP.js
import { LightningElement, wire } from 'lwc';
import getExternalIp from '@salesforce/apex/LWC_Utils.getExternalIp';
export default class ExampleIP extends LightningElement {
@wire(getExternalIp) ip;
}
LWC_Utils.cls
public class LWC_Utils {
@AuraEnabled(cacheable=true)
public static String getExternalIp() {
return Auth.SessionManagement.getCurrentSession().get('SourceIp');
}
}
And by "basic" I mean that this example is "assuming all goes well", but you should actually control that there are no errors coming back from the class, and that it can be called (for example that it does not fail because the Profile does not have access to the class and the critical update that checks that is active)