keyshang / laravel-authentication-log
Laravel Authentication Log 为 Laravel 提供身份验证日志和通知。
Requires
- php: >=7.2
- laravel/framework: ^8.0
Requires (Dev)
- orchestra/testbench: ^3.5|^4.0|^5.0|^6.0
- phpunit/phpunit: ^6.0|^7.0|^8.0|^9.0
README
功能
- 记录用户登录并检索它
- 当用户从新设备登录时通过电子邮件发送通知
- 通知电子邮件可以通过设置区域设置进行翻译。并且已经有了英文和中文翻译。
变更日志
2.0.0
灵感来源于 yadahan/laravel-authentication-log。
为了使包简单、干净,移除了不必要的注销记录功能、Slack 通知和 NexmoMessage 通知。
添加可翻译的电子邮件功能,并添加中文翻译。欢迎推送更多语言的翻译。
添加一些其他代码改进。
安装
要求:Laravel 8.x,PHP 7.2+。
-
使用 Composer 安装
composer require keyshang/laravel-authentication-log
-
迁移数据库。Laravel Authentication Log 迁移将创建存储身份验证日志所需的表
php artisan migrate
-
将
AuthenticationLogable
和Notifiable
特性添加到您的可验证模型中(默认为App\User
模型)。这些特性提供了各种方法,允许您获取常见的身份验证日志数据,例如最后登录时间、最后登录 IP 地址,并设置在从新设备登录时通知用户的方式
use Illuminate\Notifications\Notifiable; use KeyShang\AuthenticationLog\AuthenticationLogable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, AuthenticationLogable; }
覆盖默认配置
运行以下命令,并更改 config/authentication-log.php
中生成的文件。
php artisan vendor:publish --tag=authentication-log-config
通知新设备登录
默认情况下,电子邮件通知是启用的。
您可以通过将 config/authentication-log.php
配置文件中的 notify
选项设置为 false
来禁用电子邮件通知
'notify' => env('AUTHENTICATION_LOG_NOTIFY', false),
清除旧日志
您可以使用 authentication-log:clear
Artisan 命令清除旧的身份验证日志记录
php artisan authentication-log:clear
将删除 config/authentication-log.php
中 older
选项指定的天数的旧记录
'older' => 365,
覆盖默认视图
php artisan vendor:publish --tag=authentication-log-views
覆盖默认翻译
php artisan vendor:publish --tag=authentication-log-translations
基本用法
获取用户的全部身份验证日志
User::find(1)->authentications;
获取用户最后登录信息
User::find(1)->lastLoginAt(); User::find(1)->lastLoginIp();
获取用户之前登录时间及 IP 地址(忽略当前登录)
auth()->user()->previousLoginAt(); auth()->user()->previousLoginIp();
预览邮件通知
在路由文件中,添加以下代码以预览您的邮件模板。
Route::get('/notification', function () { $log = \KeyShang\AuthenticationLog\AuthenticationLog::first(); $user = $log->authenticatable; return (new \KeyShang\AuthenticationLog\Notifications\NewDevice($log)) ->toMail($user); });
贡献
感谢您考虑为 Laravel Authentication Log 贡献。
许可协议
Laravel Authentication Log 是开源软件,许可协议为 MIT 许可协议。