exodusanto/laravel-concierge

laravel项目的API令牌认证

v0.6.0 2022-10-07 13:10 UTC

This package is auto-updated.

Last update: 2024-09-07 17:21:26 UTC


README

Latest Version on Packagist Build Status Total Downloads

通过一些新功能扩展Laravel的基座解决方案

  • GET请求上自动刷新用户令牌
  • 刷新/撤销方法
  • Blade指令

安装

您可以通过composer安装此包

composer require exodusanto/laravel-concierge

使用方法

1. 迁移

使用api_tokenapi_token_refreshed_at迁移您的用户表。

Schema::table('users', function (Blueprint $table) {
    $table->string('api_token')->nullable();
    $table->timestamp('api_token_refreshed_at')->nullable();
});

api_token_refreshed_at

该属性用于存储api_token更新的时间戳

2. 模型

RefreshApiToken特质和RefreshApiTokenContract契约添加到您的模型中

class User extends BaseUser implements RefreshApiTokenContract
{
    use RefreshApiToken;
}

3. 配置

发布concierge配置

php artisan vendor:publish --tag=concierge-config

auth.providersconcierge.tokens_lifetime中使用相同的密钥来识别正确的模型

// config/auth.php
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
]

// config/concierge.php
'tokens_lifetime' => [
    'users' => 10800 // 3h
]

4. 中间件

RefreshApiToken添加到您的应用web中间件组

// app/Http/Kernel.php
protected $middlewareGroups = [
    'web' => [
        ...
        \Exodusanto\Concierge\Http\Middleware\RefreshApiToken::class,
    ],

@Concierge

Concierge附带了一个自定义Blade指令,它将渲染认证用户的令牌

@concierge

<!-- Rendered to -->
<script>
    __CONCIERGE__ = { "api_token": "XXXXXXXXXXXX" }
</script>

@Concierge 选项

@concierge($guard, $attributeName)

@concierge('other_guard', 'my_token')

<!-- Rendered to -->
<script>
    <!-- Token of other_guard authenticated user -->
    __CONCIERGE__ = { "my_token": "XXXXXXXXXXXX" }
</script>

测试

composer test

变更日志

请参阅CHANGELOG以获取更多关于最近更改的信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过info@antoniodalsie.com发送电子邮件,而不是使用问题跟踪器。

致谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。

Laravel包模板

此包是使用Laravel包模板生成的。