anwarelbo/laravel-authentication-log

Laravel Authentication Log为Laravel提供认证日志和通知功能。

v1.4.3 2021-08-04 01:14 UTC

This package is not auto-updated.

Last update: 2024-09-12 14:54:32 UTC


README

Build Status StyleCI Quality Score Total Downloads GitHub license

安装

Laravel Authentication Log需要Laravel 5.5或更高版本,以及PHP 7.0+。

您可以使用Composer将Laravel Authentication Log安装到您的Laravel项目中

composer require yadahan/laravel-authentication-log

配置

安装Laravel Authentication Log后,使用vendor:publish Artisan命令发布其配置、迁移和视图

php artisan vendor:publish --provider="Yadahan\AuthenticationLog\AuthenticationLogServiceProvider"

接下来,您需要迁移数据库。Laravel Authentication Log迁移将创建应用程序存储认证日志所需的表

php artisan migrate

最后,将AuthenticationLogableNotifiable特质添加到您的认证模型(默认为App\User模型)中。这些特质提供各种方法,让您获取常见的认证日志数据,例如最后登录时间、最后登录IP地址,并设置当从新设备登录时通知用户的消息渠道

use Illuminate\Notifications\Notifiable;
use Yadahan\AuthenticationLog\AuthenticationLogable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, AuthenticationLogable;
}

基本用法

获取用户的全部认证日志

User::find(1)->authentications;

获取用户最后登录信息

User::find(1)->lastLoginAt();

User::find(1)->lastLoginIp();

获取用户上次登录时间及IP地址(忽略当前登录)

auth()->user()->previousLoginAt();

auth()->user()->previousLoginIp();

通知新设备登录

通知可以通过mailnexmoslack渠道发送。默认通过电子邮件发送。

您可以通过定义notifyAuthenticationLogVia方法来确定通知应该通过哪些渠道发送

/**
 * The Authentication Log notifications delivery channels.
 *
 * @return array
 */
public function notifyAuthenticationLogVia()
{
    return ['nexmo', 'mail', 'slack'];
}

当然,您可以通过在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,

贡献

感谢您考虑为Laravel Authentication Log做出贡献。

许可证

Laravel Authentication Log是开源软件,采用MIT许可证