PHPackages                             pearldrift/laravel-authentication-log - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. pearldrift/laravel-authentication-log

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

pearldrift/laravel-authentication-log
=====================================

Log user authentication details and send new device notifications.

v1.0(4y ago)211MITPHPPHP ^7.4|^8.0

Since Jan 7Pushed 4y ago1 watchersCompare

[ Source](https://github.com/pearldrift/laravel-authentication-log)[ Packagist](https://packagist.org/packages/pearldrift/laravel-authentication-log)[ Docs](https://github.com/pearldrift/laravel-authentication-log)[ RSS](/packages/pearldrift-laravel-authentication-log/feed)WikiDiscussions main Synced 3d ago

READMEChangelog (1)Dependencies (8)Versions (2)Used By (0)

[![Package Logo](https://camo.githubusercontent.com/21aee1b49b8723cb3a759843b3c8da5f85f55c2b37dcf597d3a300e08a4abe21/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c25323041757468656e7469636174696f6e2532304c6f672e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d706561726c64726966742532466c61726176656c2d61757468656e7469636174696f6e2d6c6f67267061747465726e3d686964656f7574267374796c653d7374796c655f31266465736372697074696f6e3d4c6f672b757365722b61757468656e7469636174696f6e2b64657461696c732b616e642b73656e642b6e65772b6465766963652b6e6f74696669636174696f6e732e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d6c6f636b2d636c6f736564)](https://camo.githubusercontent.com/21aee1b49b8723cb3a759843b3c8da5f85f55c2b37dcf597d3a300e08a4abe21/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c25323041757468656e7469636174696f6e2532304c6f672e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d706561726c64726966742532466c61726176656c2d61757468656e7469636174696f6e2d6c6f67267061747465726e3d686964656f7574267374796c653d7374796c655f31266465736372697074696f6e3d4c6f672b757365722b61757468656e7469636174696f6e2b64657461696c732b616e642b73656e642b6e65772b6465766963652b6e6f74696669636174696f6e732e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d6c6f636b2d636c6f736564)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3375b7cdb3d121958d3e1b00b8f3069c461ca40bca0b3cf51e7199291b507a75/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706561726c64726966742f6c61726176656c2d61757468656e7469636174696f6e2d6c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pearldrift/laravel-authentication-log)[![GitHub Tests Action Status](https://camo.githubusercontent.com/3d4ea370407f056f4138ed2d6f7338f4cc078fc84d110be7aed47e8faff1ebd4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f706561726c64726966742f6c61726176656c2d61757468656e7469636174696f6e2d6c6f672f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/pearldrift/laravel-authentication-log/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/7c32a075d80397c066014d594880a0f2078fe71988e48b4034d92e98255f5f67/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f706561726c64726966742f6c61726176656c2d61757468656e7469636174696f6e2d6c6f672f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/pearldrift/laravel-authentication-log/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/4b13e85782d47ddef3fd582222a8327708f9be3b68408247051cd933e83f79bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f706561726c64726966742f6c61726176656c2d61757468656e7469636174696f6e2d6c6f672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pearldrift/laravel-authentication-log)

Laravel Authentication Log is a package which tracks your user's authentication information such as login/logout time, IP, Browser, Location, etc. as well as sends out notifications via mail, slack, or sms for new devices and failed logins.

Documentation, Installation, and Usage Instructions
---------------------------------------------------

[](#documentation-installation-and-usage-instructions)

Installation
------------

[](#installation)

> Laravel Authentication Log requires Laravel 5.5 or higher, and PHP 7.0+.

You may use Composer to install Laravel Authentication Log into your Laravel project:

```
  composer require pearldrift/laravel-authentication-log

  composer require torann/geoip

```

### Configuration

[](#configuration)

After installing the Laravel Authentication Log, publish its config, migration and view, using the `vendor:publish` Artisan command:

```
  php artisan vendor:publish --provider="Pearldrift\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-migrations"

```

Next, you need to migrate your database. The Laravel Authentication Log migration will create the table your application needs to store authentication logs:

```
   php artisan migrate

```

You can publish the view/email files with:

```
  php artisan vendor:publish --provider="Pearldrift\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-views"

```

Finally, add the `AuthenticationLogable` and `Notifiable` traits to your authenticatable model (by default, `App\User` model). These traits provides various methods to allow you to get common authentication log data, such as last login time, last login IP address, and set the channels to notify the user when login from a new device:

You can publish the config file with:

```
     php artisan vendor:publish --provider="Pearldrift\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-config"

```

This is the contents of the published config file:

```
    return [
      // The database table name
      // You can change this if the database keys get too long for your driver
      'table_name' => 'authentication_log',

      // The database connection where the authentication_log table resides. Leave empty to use the default
      'db_connection' => null,

      // The events the package listens for to log (as of v1.3)
      'events' => [
          'login' => \Illuminate\Auth\Events\Login::class,
          'failed' => \Illuminate\Auth\Events\Failed::class,
          'logout' => \Illuminate\Auth\Events\Logout::class,
          'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
      ],

      'notifications' => [
          'new-device' => [
              // Send the NewDevice notification
              'enabled' => env('NEW_DEVICE_NOTIFICATION', true),

              // Use torann/geoip to attempt to get a location
              'location' => true,

              // The Notification class to send
              'template' => \Pearldrift\LaravelAuthenticationLog\Notifications\NewDevice::class,
          ],
          'failed-login' => [
              // Send the FailedLogin notification
              'enabled' => env('FAILED_LOGIN_NOTIFICATION', false),

              // Use torann/geoip to attempt to get a location
              'location' => true,

              // The Notification class to send
              'template' => \Pearldrift\LaravelAuthenticationLog\Notifications\FailedLogin::class,
          ],
      ],

      // When the clean-up command is run, delete old logs greater than `purge` days
      // Don't schedule the clean-up command if you want to keep logs forever.
      'purge' => 365,
  ];

```

If you installed torann/geoip you should also publish that config file to set your defaults:

```
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config

```

Setting up your model
---------------------

[](#setting-up-your-model)

You must add the **AuthenticationLoggable** and **Notifiable** traits to the models you want to track.

```
use Illuminate\Notifications\Notifiable;
use Pearldrift\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, AuthenticationLoggable;
}

```

The package will listen for Laravel's Login, Logout, Failed, and OtherDeviceLogout events.

Overriding default Laravel events
---------------------------------

[](#overriding-default-laravel-events)

If you would like to listen to your own events you may override them in the package config (as of v1.3).

Example event override
----------------------

[](#example-event-override)

You may notice that Laravel - [fires a Login event when the session renews](https://github.com/laravel/framework/blob/master/src/Illuminate/Auth/SessionGuard.php#L149) if the user clicked 'remember me' when logging in. This will produce empty login rows each time which is not what we want. The way around this is to fire your own Login event instead of listening for Laravels.

You can create a Login event that takes the user:

```
