-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expose event.clientAddress
#4289
Conversation
🦋 Changeset detectedLatest commit: a548fa8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
I just checked, and on Vercel const [clientAddress] = headers.get('x-forwarded-for').split(','); |
Also, in 1fa6823 |
|
Thank you @Rich-Harris, I think it perfect now. |
Would be awesome if someone could make an example as to how/where to use it. I'd like to have the IP address available in my svelte component - where exactly do I start? Where can I find the |
Hey guys, this returns an invalid address on our production servers. Using node express (same server groups) returns a valid address so something is broken with @AlessioGr yeah they are bad at giving examples for people learning.
|
@AlessioGr I made an example of how to get a user's ip address and subsequently the user's location using sveltekit. Repo - https://github.com/CAPTAIN320/sveltekit-user-ip-location-example |
@CAPTAIN320 I see you're exporting a variable on hooks.server.ts. Since it runs on the server side, isn't the variable shared by all clients? The correct approach should be getting the ip in the |
@a4vg Getting the variable from the load function and having your app is deployed to a Paas such as Vercel, will result in the location of the vercel server being returned. Which was why I was getting US location for all my users. From then on you can import it to any *.server.ts file. |
@CAPTAIN320 Maybe the solution works, but you should write the IP to Now, for anyone else who can't still get the public IP address... I was using Heroku FWIW, but I had the same issue where I couldn't get the real IP. I'm using the node-adapter and after reading this note on the docs:
I added // +page.server.ts
export const load: PageServerLoad = ({ request }) => {
const ip = request.headers.get('x-forwarded-for') || ''; Another option is setting the env var The node-adapter code shows exactly how kit/packages/adapter-node/src/handler.js Lines 131 to 136 in 96993cf
|
setting the |
I'm a beginner., getclientaddress() is reporting 127.0.0.1 How do you do that? Where do you set the env var ADDRESS_HEADER? @Carlos-err406 |
i did the test on a remote server as for where to set the enviroment variable it was on my root .env as a regular enviroment variable, no further implications were required |
That was detailed and specific. Impressive and helpful. Thank you. |
Here is a more complete example for anyone that it helps...
/** @type {import('./$types').PageServerLoad} */
export const load = async (event) => {
let requestIp;
try {
requestIp = event.getClientAddress(); // IP from Client Request
console.log('IP Address from Client Request: ', requestIp);
} catch (error) {
console.log('Error reading IP');
}
return { clientAddress: requestIp }
}
<script>
import { onMount } from 'svelte';
/** @type {import('./$types').PageData} */
export let data;
onMount(async () => {
await import('@mux/mux-player');
});
</script>
<mux-player
playback-id="..."
metadata-viewer-user-id="{data.clientAddress}"
></mux-player> This worked deployed to Vercel with Didn't seem to need to add any environment config like |
anyone that's trying to figure out how to do this in 2024 from an API endpoint in Svelte 5, it's like this
Save yourself some time. |
Supersedes #3993. This adds a new property to
event
,clientAddress
, which adapters are expected to populate with the client IP address.Will be very grateful for any contributions to this PR!
clientAddress
in dev and previewx-forwarded-for
may) and whether we should split themWarn on startup ifchanged my mindgetClientAddress
is not providedevent.clientAddress
ifgetClientAddress
is not providedPlease don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpx changeset
and following the prompts. All changesets should bepatch
until SvelteKit 1.0