Ad

Alter Laravel's Default Log In Method?

- 1 answer

I generated authentication controllers and routes using the php artisan make:auth command.

I would like to update a field named last_login in my database whenever a user logs in.

Ad

Answer

I've altered the default Auth to provide the ability to convert users' passwords from an old algorithm to bcrypt (i'm refactoring a legacy app).

The way I did it:

in app\Providers\EventServiceProvider.php:

'Illuminate\Auth\Events\Login' => [
     'App\Listeners\LogAuth',
],

i then added the app\Listeners\LogAuth.php file with the following contents

<?php
namespace App\Listeners;

use Illuminate\Auth\Events\Attempting;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Auth, App\User, Hash;

class LogAuth {

    public function __construct()
    {
        //
    }

    public function handle($credentials, $remember, $login)
    {
        // get the user, update the column, save
    }
}

I hope this helps.

Ad
source: stackoverflow.com
Ad