spargon/laravel-auth-logger
Composer 安装命令:
composer require spargon/laravel-auth-logger
包简介
Laravel Auth Logger stores user authentication logs and sends out notifications whenever a user logs in from a new system.
README 文档
README
Laravel Auth Logger stores user authentication logs and sends out notifications whenever a user logs in from a new system.
Installation
Laravel Auth Logger requires PHP 7.0+ and currently supports Laravel 7, 8 & 9.
You can install the package via composer:
composer require spargon/laravel-auth-logger
After installing the Laravel-Auth-Logger package, you need run the install command which will take care of everything you need to get started. Type the following artisan command in your console:
php artisan auth-logger:install
This will publish the config/auth-logger.php file, publish the necessary migration files and ask you for permission to run said migrations.
Once installed, you need to add the AuthLogable and Notifiable traits to your authenticatable model (by default its, App\Models\User model). These traits provides you with various methods to get the data generated by the auth logger, such as last login time, last login IP address, and sets the channels to notify the user when they login from a new device:
use Illuminate\Notifications\Notifiable; use Spargon\AuthLogger\AuthLogable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, AuthLogable; }
Usage
Get all the authentication logs for a user:
User::find(1)->auths;
Get the user's last login info (includes the current session if the user is logged in):
User::find(1)->lastLoginAt(); [or] auth()->user()->lastLoginAt() User::find(1)->lastLoginIp(); [or] auth()->user()->lastLoginIp()
Get the user's previous login time & ip address (ignoring the current login log):
User::find(1)->previousLoginAt(); [or] auth()->user()->previousLoginAt() User::find(1)->previousLoginIp(); [or] auth()->user()->lapreviousLoginIpstLoginAt()
Send New Device Alert Notification
Notifications may be sent on the mail, or/and the slack channels. By default AuthLogger will send a notification via the mail driver (provided you have enabled notify in config/auth-logger.php file).
You can define notifyAuthLoggerVia method on the User's Model to determine which channels the notification should be sent to:
/** * The Auth Logger notifications delivery channels. * * @return array */ public function notifyAuthLoggerVia() { return ['mail', 'slack']; }
Slack Notifications & Customiziations
Note: If you want to use the slack notification channel, you need to install the laravel/slack-notification-channel package first otherwise your app will throw a Driver not supported error. Be sure to go through the Laravel docs for Slack Pre-requisites here.
You also need to generate a Slack Incoming Webhook from here and then add the generated webhook to your authenticatable model (usually the User) as such:
/** * Route notifications for the Slack channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForSlack($notification) { return 'https://hooks.slack.com/services/T01D8HUCU4.....'; // replace this with the webhook you receive from Slack }
Customizing Messages
In config/auth-logger.php file, you can customize the name of the Sender, the Channel to receive the notifications on & the Icon to show on the notification.
By default all slack messages will be sent to the #general channel.
| Slack Sample Image | Mail Sample Image |
|---|---|
![]() |
![]() |
Disabling Notifications
Of course you can disable notifications by setting the notify option in your config/auth-logger.php configuration file to false:
'notify' => env('AUTH_LOGGER_NOTIFY', false),
User Agent Parser
This package uses the jenssegers/agent package to parse the user's user-agent when sending the alert notification. Take a look at their docs here to learn how to use it in your app (ex: in your logs table).
Delete logs after a while
You may clear the old authentication log records using the auth-logger:clear Artisan command:
php artisan auth-logger:clear
Records that are older than the number of days specified in the older option in your config/auth-logger.php will be deleted when you run the above command:
'older' => 31,
Optional Files
You can publish the New Device Alert Email view using the command below:
php artisan vendor:publish --tag=auth-logger-views
You can also publish the translations file used by the auth-logger using the command below:
php artisan vendor:publish --tag=auth-logger-translations
These are optional files. You don't need to publish them for the package to work. They exist only for cases where you want to make any changes to the files yourself.
Change Database Table Name
If you want to change the name of the auth-logger table, you can do so by changing the vaule of table_name in the config/auth-logger.php file (this step is optional - you must also update the table name in the migrations/database to reflect the same).
Experimental (dev-geoip)
Currently we are experimenting with an implention of Location Tagging (using GeoIP package from Torann). You can checkout the geoip branch to play around with it or get the dev-geoip release from packagist using composer require spargon/laravel-auth-logger:dev-geoip
This is an experimental release of location tagging. Accuracy is not 100% guaranteed. Use it at your own risk. PS> This experimental version does not contain the install command.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
spargon/laravel-auth-logger 适用场景与选型建议
spargon/laravel-auth-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 203 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「Authentication」 「notification」 「laravel」 「spargon」 「laravel-auth-logger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spargon/laravel-auth-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spargon/laravel-auth-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spargon/laravel-auth-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package makes it easy to send notifications via AfricasTalking with Laravel
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Apple Push Notification & Feedback Provider
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Throttle notifications on a per-channel basis
统计信息
- 总下载量: 203
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-21


