Skip to content

Commit d4a06ce

Browse files
committed
Extracted extractClientIp and used it login.magic and admin.server.ts
1 parent acbc061 commit d4a06ce

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

apps/webapp/app/models/admin.server.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ import {
99
setImpersonationId,
1010
} from "~/services/impersonation.server";
1111
import { requireUser } from "~/services/session.server";
12-
13-
function extractClientIp(xff: string | null): string | null {
14-
if (!xff) return null;
15-
const parts = xff.split(",").map((p) => p.trim());
16-
return parts[parts.length - 1]; // ALB appends the real client IP
17-
}
12+
import { extractClientIp } from "~/utils/extractClientIp.server";
1813

1914
const pageSize = 20;
2015

21-
export async function adminGetUsers(userId: string, { page, search }: SearchParams) {
16+
export async function adminGetUsers(
17+
userId: string,
18+
{ page, search }: SearchParams,
19+
) {
2220
page = page || 1;
2321

2422
search = search ? decodeURIComponent(search) : undefined;

apps/webapp/app/routes/login.magic/route.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from "~/services/magicLinkRateLimiter.server";
3131
import { logger, tryCatch } from "@trigger.dev/core/v3";
3232
import { env } from "~/env.server";
33+
import { extractClientIp } from "~/utils/extractClientIp.server";
3334

3435
export const meta: MetaFunction = ({ matches }) => {
3536
const parentMeta = matches
@@ -169,13 +170,6 @@ export async function action({ request }: ActionFunctionArgs) {
169170
}
170171
}
171172

172-
const extractClientIp = (xff: string | null) => {
173-
if (!xff) return null;
174-
175-
const parts = xff.split(",").map((p) => p.trim());
176-
return parts[parts.length - 1]; // take last item, ALB appends the real client IP by default
177-
};
178-
179173
export default function LoginMagicLinkPage() {
180174
const { magicLinkSent, magicLinkError } = useTypedLoaderData<typeof loader>();
181175
const navigate = useNavigation();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Extracts the client IP address from the X-Forwarded-For header.
3+
* Takes the last item in the header since ALB appends the real client IP by default.
4+
*/
5+
export function extractClientIp(xff: string | null): string | null {
6+
if (!xff) return null;
7+
8+
const parts = xff.split(",").map((p) => p.trim());
9+
return parts[parts.length - 1]; // take last item, ALB appends the real client IP by default
10+
}

0 commit comments

Comments
 (0)