F
FilamentWEBMAS

How to record the last login (online) time of a user?

The user table has created_at and updated_at fields. I want to add a logged_at field. To track when the user was last on the site. How to do this using Laravel and Filament? Or how to do it correctly?
P
Patrick198941d ago
This is not really filament specific, this page should get you going https://laraveldaily.com/post/save-users-last-login-time-ip-address
W
WEBMAS41d ago
Is there a plugin where the history of logins (time, IP, location, etc.) and active sessions is stored?
P
Patrick198941d ago
certainly
P
Patrick198941d ago
Filament
Plugins - Filament
Community made packages for Filament projects, which give you access to awesome new features.
W
WEBMAS41d ago
Which one do you recommend?
P
Patrick198941d ago
Oh i have not used one myself just pick one you like
W
WEBMAS40d ago
How do I add the logic of updating the last_login_at and last_login_ip fields to the Filament? Where can I do this?
P
Patrick198940d ago
You dont need filament for that Listen for the authenicated event and do your logic
P
Patrick198940d ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
P
Patrick198940d ago
No description
W
WEBMAS40d ago
It's still hard for me to figure it out. Could you tell me which file to add the code to: $user->update([ 'last_login_at' => Carbon::now()->toDateTimeString(), 'last_login_ip' => $request->getClientIp() ]);
P
Patrick198940d ago
You can create a new class php artisan make:listener LogSuccessfulLogin that class will have a handle($event) method where you can put the logic
W
WEBMAS40d ago
Will it work after registration too?
P
Patrick198940d ago
that is a different event so you should listen for a different one but if you log in directly after registration it should be fine
W
WEBMAS40d ago
It is necessary that both after login and after registration the data is recorded in the fields ?
P
Patrick198940d ago
i have provided you the answers, just take the time to read the documentation
P
Patrick198940d ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
P
Patrick198940d ago
laravel has a Registered event you can listen for
W
WEBMAS40d ago
Thanks And which event is responsible for visiting the site? I want to update these fields not only when login and registration, but also when the user was last on the site. To record the last visit. What kind of event is this?
P
Patrick198940d ago
use middleware for that instead of an event middleware gets executed every request
S
SiGuy39d ago
i implented it with middleware that sets a current timestamp on the user (new column: last_activity for example). Then I check whether the last actitvity is older than 5 minutes to display whether the user is "online" or "idle" and cache the result. On the User Model you then can check for the key in the cache with the user id. if it exists, the user is online like so: /** * @return bool */ public function isOnline() : bool { return Cache::has('user-is-online-' . $this->id); } a better way for this though are websockets and presense channels!
No description
W
WEBMAS39d ago
Thank you. In this case, how can you find out what time you were last active? For example, the last_active_at field in the users table. Where and when should I fill out this field? I think we need to overwrite the last_active_at field in the middleware. And in the isOnline model, if more than 5 minutes have passed since last_active_at, then the user is not active. For example. But what to do if the user has a tab open in the browser. And he's online. But this solution will not show that it is online, because the page is not updated.
P
Patrick198939d ago
a solution would be a little javascript on the page that just sends a heartbeat to the server every X seconds
S
SiGuy39d ago
There is no "isOnline" model. "isOnline" is a method on the User Model. And the last active timestamp is set on every request that the user makes, because it is set in the middleware... you only need to register the middleware in your web stack....
W
WEBMAS39d ago
Yes, I mean the isOnline method. Using the cache for 5 minutes, I won't be able to know the exact time of the user's last activity. For example, it was active 1 or 2 days ago. Therefore, the timestamp must be stored in the database.
S
SiGuy39d ago
If you really want a "correct" implementation (user is set to offline, once he closes the browser) you would need to do it with websockets and presense channels, as stated before in my first answer... well... the last activity timestamp will contain the timestamp of the last activity, which can be 1 or 2 days old...
W
WEBMAS39d ago
But in your screenshot it uses the cache. If it gets destroyed, I won't be able to find out the timestamp.
S
SiGuy39d ago
sorry - you are absolutely right. i switched to cache because i only needed the info whether the user is online or not. But you could just exchange cache for a column on the user table. I think i mentioned this in my initial answer.... maybe it was a bit confusing, because i mixed up the implementations ^^ add a last_activty column to the user table, then in the middleware you udate the timestamp in the db for the currently logged in user (or you get the user object from the request) and on the user model you dont check the cache, but instead just check whether the TS is older than x minutes to return true/false
W
WEBMAS39d ago
middleware: update last_active_at in user table model user, method isOnline: if ($now - $...->last_active_at) < 5 min then user is online else is not online
S
SiGuy39d ago
excatly
W
WEBMAS39d ago
Where should I connect middleware LastUserActivity? In App\Http\Kernel.php or in App\Providers\Filament\AdminPanelProvider.php ?
No description
No description
S
SiGuy39d ago
depends where you want to use it. if you only need it on the filament part, i would add it to the panel... if you also want to use it outside of filament, you can add it to the kernel
W
WEBMAS39d ago
If I add it to the kernel, then will it work globally, including in the Filament?
S
SiGuy39d ago
try it. but i think you need to add it to the panel...
Want results from more Discord servers?
Add your server
More Posts
struggle with select field =_=i create a Select then for its option i have a model which has this relation: > `public function fiwhat merhod to use in import action: castStateUsing or filstateUsingHi everyone, i have a table with id and name and i want to modify name, while importing, based on a Weird login error after livewire updateAfter `composer update` I can't login to filament. I see the login form but the password field is aReusable sectionsHi, I was wondering how you usually create reusable parts for example e.g. infolists or forms? For efiltersForm()Im using custom livewire page with widget with table. I have defined filter form and $this->filters JS error : "Livewire assets are out of date"Hi guys, I made a few changes on my Filament app and now login isn't working anymore and I think it'It's possible to put the relation on top of form page?I have a big form. After create a record ( with doesn't have the relation fields already ), a user dWork with existing tables without deleting their contents when doing migrationsHi guys, I'd like to work with already existing data in the database. Unfortunately the table is deAction opening an empty state modalI'm using Action in a spatie livewire wizard component. When I click on next button once, the step iBlock Repeater Rating with MorphToMany RelationGreetings, I have a block with this form inside class Rating { public static function make( Ignore using filament:assets for packageI am using this package for inspiration: https://github.com/cheesegrits/filament-google-maps and fouHow to increase Pest test coverageHey, I am attempting to increase my test coverage to 100% for my resources and can't seem to figurHow to put images in a textarea with filament ?I've tried a lot of things but when I insert my images in a textarea, and that I go back to the fronfile uploading to S3 on vapor stuck on uploadingI have set a new server on Laravel Vapor, the S3 was generated by vapor with full public access, theUse filament Components outside filamentCan I use filament components like a table outside filament assuming I have a simple laravel app witDoes --generate flag work when creating a resource?To test it I: 1- Updated filament libs (`composer update`) & cleared cache `php artisan optimize:clMissing New panel resources - links don't appear on sidebar - 404-Not foundHello. I have been developing an app since the beginning of February and everthing was Ok. I have beNested relation formHi guys, is it possible to create form with nested relations? ```php // ClientResource.php Forms\CoDispatching a livewire event from a filament action when submitHello, I need help dispatching a livewire event from a filament action but none of the answers or meButton loading animation too fastIs this button loading animation intentionally designed like this ? i think it is too fast