Get IP Address in tRPC router
Hey guys how can I get the IP address inside a procedure?
Like this code f.ex:
Like this code f.ex:
export const contactRouter = createTRPCRouter({
hello: publicProcedure
.input(contactSchema)
.mutation(async ({ input, ctx }) => {
// get ip here
const msg = await db.contactMessage.create({
data: {
name: input.name,
email: input.email,
subject: input.subject,
message: input.message,
},
});
return { success: true, msg };
}),
});const clientIP = req.headers['x-forwarded-for'] || req.socket.remoteAddress;export async function createContext(opts: FetchCreateContextFnOptions) {
const { userId } = auth();
const ip = opts.req.headers['x-forwarded-for']
// now we can return the context
// (this will be passed to every procedure)
// so we can access the database and additionally the current user ID
return {
prisma,
clerkUserId: userId,
};
}req.headers['x-forwarded-for']req.ip
req.connection.remoteAddressconst ip = opts.req.headers['x-forwarded-for']const clientIP = req.headers['x-forwarded-for'] || req.socket.remoteAddress;