Access client IP address in server action #49730
-
|
I've been experimenting with server actions, since I like the progressive enhancement approach. For auditing reasons, I need to access the IP address of the client making the request. I haven't seem able to find a way to access this, is it possible? If I'm not mistaken, the function only gets passed the formData, and nothing else. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
|
Usually, there is a proxy server in front of the node application. You can set header there (for example, proxy_set_header for nginx and read |
Beta Was this translation helpful? Give feedback.
-
|
Having this problem as well. Thought that this would do the trick, but to no avail. I guess the downside of the simplification of server actions is some loss of advanced functionality like this. Would be nice if we could somehow access the standard req and res objects that are available on typical routes. |
Beta Was this translation helpful? Give feedback.
-
|
Updated the docs here to make it more clear this depends on the infra provider, but you can also read some things from headers if provided: #59719 |
Beta Was this translation helpful? Give feedback.
-
|
i forked request-ip for nextjs / Headers friendly https://www.npmjs.com/package/next-request-ip import { getClientIp } from "next-request-ip";
import { headers } from "next/headers";
export default async function Page() {
const headersList = await headers();
const clientIp = getClientIp(headersList);
return (
<div>
<p>Your IP: {clientIp}</p>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
Updated the docs here to make it more clear this depends on the infra provider, but you can also read some things from headers if provided: #59719