41 lines
916 B
TypeScript
41 lines
916 B
TypeScript
import type { FastifyInstance, FastifyRequest } from 'fastify'
|
|
import {
|
|
appendAudit,
|
|
type AppendAuditInput,
|
|
} from '@authportal/db'
|
|
|
|
export function clientIp(request: FastifyRequest): string | null {
|
|
// request.ip учитывает X-Forwarded-For только от доверенных прокси (TRUST_PROXY).
|
|
return request.ip ?? null
|
|
}
|
|
|
|
export function safeAudit(
|
|
app: FastifyInstance,
|
|
input: AppendAuditInput,
|
|
): void {
|
|
try {
|
|
appendAudit(app.db, input)
|
|
} catch (err) {
|
|
app.log.warn({ err }, 'audit_log append failed')
|
|
}
|
|
}
|
|
|
|
export function actorFromRequest(request: FastifyRequest): Pick<
|
|
AppendAuditInput,
|
|
'actorUserId' | 'actorEmail' | 'actorName'
|
|
> {
|
|
const u = request.authUser
|
|
if (!u) {
|
|
return {
|
|
actorUserId: null,
|
|
actorEmail: null,
|
|
actorName: null,
|
|
}
|
|
}
|
|
return {
|
|
actorUserId: u.id,
|
|
actorEmail: u.email,
|
|
actorName: u.name,
|
|
}
|
|
}
|