How to Save User's Last Login Time and IP Address

Tutorial last revisioned on August 22, 2022 with Laravel 9

Quick tip of the day. Default Laravel Auth comes with User table and model, but without logging capability, so we need to build it ourselves. Fortunately, it's very easy, I will show you one method.

Let's say that we want to save user's last login time and IP address in the same users table. So we start with database migration:

php artisan make:migration add_login_fields_to_users_table

Then we fill it with these fields:

return new class extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->datetime('last_login_at')->nullable();
$table->string('last_login_ip')->nullable();
});
}
// ...

Next, we need to add these fields as fillables in app/Models/User.php model:

class User extends Authenticatable
{
protected $fillable = [
'email',
'password',
'name',
'last_login_at',
'last_login_ip',
];
 
// ...

Finally, how do we fill them in? Well, it depends how on which starter kit you are using. I'm going to show you that using Laravel Breeze, Laravel Jetstream), and Laravel UI.


Laravel Breeze

Breeze uses the app/Http/Controllers/Auth/AuthenticatedSessionController.php controller for authentication. In that controller, there is a store method, which is called after you press login button. Just before redirecting, after successful authentication, let's add our code for the update.

$request->user()->update([
'last_login_at' => Carbon::now()->toDateTimeString(),
'last_login_ip' => $request->getClientIp()
]);

Laravel Jetstream

Jetstream uses Laravel Fortify for handling authentication. To save additional data, we will need to customize users authentication. Typically this should be done in the JetstreamServiceProvider file, in the boot method.

public function boot()
{
// ...
 
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->email)->first();
 
if ($user &&
Hash::check($request->password, $user->password)) {
 
$user->update([
'last_login_at' => Carbon::now()->toDateTimeString(),
'last_login_ip' => $request->getClientIp()
]);
 
return $user;
}
});
}

Laravel UI

You need to know that there is authenticated() method in the AuthenticatesUsers trait. It's called every time someone logs in.

/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
//
}

In trait this method is empty. So we have freedom to override it in LoginController and add whatever code we need.

app/Http/Controllers/Auth/LoginController.php:

function authenticated(Request $request, $user)
{
$user->update([
'last_login_at' => Carbon::now()->toDateTimeString(),
'last_login_ip' => $request->getClientIp()
]);
}

And, that's it! Here's what we have in users table after log in:

img

avatar

Saved me a lot of time - Thanks! :)

avatar

Thanks for this One thing I noticed was that when a user performs a password reset (/password/reset) Laravel 10 - the authenticated does not fire - IE I have no entry for last_*

avatar

i think beacuse you don't pass in the same fortify controller methiod....from what i understood

avatar

As always, excellent explanations

avatar

i am looking to Save User's Last Login Time and IP Address using Filament

avatar

For that you need to create a new Filament Login page and add the logic there

avatar

how i want user device so what can i do ? my english weak

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 68 courses (1188 lessons, total 43 h 18 min)
  • 90 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent New Courses