定制 skofi/laravel-jwt-auth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

skofi/laravel-jwt-auth

最新稳定版本:1.0

Composer 安装命令:

composer require skofi/laravel-jwt-auth

包简介

JWT Authentication with Mail confirmation for Laravel

README 文档

README

This repository provides full functionality for authorization and mail confirmation using JWT (JSON Web Tokens) in a Laravel application.

Installation

To set up JWT authentication and mail confirmation, follow these steps:

  1. Run the following command to install the skofi/laravel-jwt-auth package:

    composer require skofi/laravel-jwt-auth
  2. Run the following command to install JWT authentication:

    php artisan jwt-auth:install
  3. Publish the vendor for JWT authentication:

    php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

Middleware for JWT

Add the following middleware for JWT authentication in your Kernel.php:

protected $middlewareAliases = [
    // ...
    'auth.jwt' => \Tymon\JWTAuth\Http\Middleware\Authenticate::class,
];

User Model Implementation

Implement the JWTSubject interface in the User model as follows:

use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    // ... (other user model code)

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

Password Reset Route Configuration

In the AuthServiceProvider of the boot() method, add the following code to configure the password reset route to redirect to the frontend URL:

use Illuminate\Auth\Notifications\ResetPassword;

// ... (other imports and code)

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any authentication / authorization services.
    */
    public function boot(): void
    {
        ResetPassword::createUrlUsing(function ($user, string $token) {
            return config('app.frontend_url') . '/reset-password?token=' . $token . '&email=' . $user->email;
        });
    }
    // ...
}

Time-to-live JWT

You can define variables in your .env file to customize the token's time-to-live (TTL) settings:

JWT_TTL=1440
JWT_REMEMBER_TTL=525600
  • JWT_TTL denotes the standard token lifetime in minutes.
  • JWT_REMEMBER_TTL represents the token's lifetime when the 'Remember me' option is selected, also in minutes.

Generate secret key

This key serves as the signature for your tokens, and the process will depend on the algorithm you choose to use.

php artisan jwt:secret

Usage

  • Once the setup is complete, you can use JWT authentication for user authorization.
  • Password reset links will be generated based on the configured FRONTEND_URL= in your .env.

Contribution

Feel free to contribute by opening issues or submitting pull requests.

License

This project is licensed under the MIT License.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-01-04