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:

1php artisan make:migration add_login_fields_to_users_table

Then we fill it with these fields:

1return new class extends Migration
2{
3 public function up()
4 {
5 Schema::table('users', function (Blueprint $table) {
6 $table->datetime('last_login_at')->nullable();
7 $table->string('last_login_ip')->nullable();
8 });
9 }
10// ...

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

1class User extends Authenticatable
2{
3 protected $fillable = [
4 'email',
5 'password',
6 'name',
7 'last_login_at',
8 'last_login_ip',
9 ];
10 
11 // ...

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.

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

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.

1public function boot()
2{
3 // ...
4 
5 Fortify::authenticateUsing(function (Request $request) {
6 $user = User::where('email', $request->email)->first();
7 
8 if ($user &&
9 Hash::check($request->password, $user->password)) {
10 
11 $user->update([
12 'last_login_at' => Carbon::now()->toDateTimeString(),
13 'last_login_ip' => $request->getClientIp()
14 ]);
15 
16 return $user;
17 }
18 });
19}

Laravel UI

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

1/**
2 * The user has been authenticated.
3 *
4 * @param \Illuminate\Http\Request $request
5 * @param mixed $user
6 * @return mixed
7 */
8protected function authenticated(Request $request, $user)
9{
10 //
11}

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:

1function authenticated(Request $request, $user)
2{
3 $user->update([
4 'last_login_at' => Carbon::now()->toDateTimeString(),
5 'last_login_ip' => $request->getClientIp()
6 ]);
7}

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

img

No comments or questions yet...

Like our articles?

Become a Premium Member for $129/year or $29/month

Written by

You might also like