mitydigital / statamic-logger

Detailed, customisable and human-friendly audit logging for Statamic
MIT License
2 stars 1 forks source link

Add Client/User IP on User Login Events #9

Open JorisOrangeStudio opened 3 weeks ago

JorisOrangeStudio commented 3 weeks ago

Is it possible to add the IP address of a User when he/she logs in? I tried it by making a customer Listener and subscribe it, but it will only return 127.0.0.1 for each Log (also on a live server). What is the best way of implementing this? Since it can be crucial information for account security.

martyf commented 1 day ago

Apologies for the delay, this slipped past me.

What are you using to get the IP address?

JorisOrangeStudio commented 1 day ago

Apologies for the delay, this slipped past me.

What are you using to get the IP address?

Hi! No problem. We tried to get it using the Request IP:

class UserLoggedInListener extends \MityDigital\StatamicLogger\Listeners\User

{
    private Request $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function supplement(mixed $event): array
    {
        return [
            'ip' => $this->request->ip()
        ];
    }
}

But this is always returning the local IP, no matter what environment (local or server) or network is used to log in. Probably because the Request is coming from the Job for this eventlistener/subscriber. Any idea how we can catch the real IP of a user that Logged In?

martyf commented 15 hours ago

It's the nature of queues unfortunately - they have no concept of where it came from by the time it gets handled.

The issue is that level of data needs to be stored with the event, not the handler. You could try re-binding events (i.e. extending the events you need more data for) but I've not been successful in doing that for some reason (I've re-bound things before but never an event, and can't get the same idea working for events, such as Laravel's Login event). But theoretically that could (should) work.