Skip to content

Commit

Permalink
log ip address during next requests (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBigBoss authored Jul 4, 2024
1 parent 8b3fa7a commit 37d2716
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
11 changes: 6 additions & 5 deletions apps/next/utils/logRequest.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { GetServerSidePropsContext } from 'next'

export function logRequest(ctx: GetServerSidePropsContext) {
console.log(
`${ctx.req.url} - ${ctx.req.headers['user-agent']}${
ctx.req.headers['x-forwarded-for'] ? ` - ${ctx.req.headers['x-forwarded-for']}` : ''
}`
)
const ip =
(ctx.req.headers['cf-connecting-ip'] ||
ctx.req.headers['x-forwarded-for'] ||
ctx.req.socket.remoteAddress) ??
''
console.log(`${ctx.req.url} - ${ctx.req.headers['user-agent']} - ${ip}`)
}
13 changes: 7 additions & 6 deletions packages/api/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import type { CreateNextContextOptions } from '@trpc/server/adapters/next'
import superJson from 'superjson'

export const createTRPCContext = async (opts: CreateNextContextOptions) => {
console.log(
`${opts.req.url} - ${opts.req.headers['user-agent']}${
opts.req.headers['x-forwarded-for'] ? ` - ${opts.req.headers['x-forwarded-for']}` : ''
}`
)
const ip =
(opts.req.headers['cf-connecting-ip'] ||
opts.req.headers['x-forwarded-for'] ||
opts.req.socket.remoteAddress) ??
''
console.log(`${opts.req.url} - ${opts.req.headers['user-agent']} - ${ip}`)

// if there's auth cookie it'll be authenticated by this helper
const supabase = createPagesServerClient<Database>(opts)

// native sends these instead of cookie auth
if (opts.req.headers.authorization && opts.req.headers['refresh-token']) {
// native sends these instead of cookie auth
const accessToken = opts.req.headers.authorization.split('Bearer ').pop()
const refreshToken = opts.req.headers['refresh-token']
if (accessToken && typeof refreshToken === 'string') {
Expand Down

0 comments on commit 37d2716

Please sign in to comment.