定制 wamesk/laravel-auth 二次开发

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

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

wamesk/laravel-auth

Composer 安装命令:

composer require wamesk/laravel-auth

包简介

OAuth2 authorization with API endpoints. Also includes registration process, login, password reset, email validation.

README 文档

README

Sanctum authorization with API endpoints.

Also includes registration process, login, password reset, email validation.

⚠️ Social login — not functional

Social login (both the JWT POST /login/{provider} endpoint and the Socialite OAuth flow) is not functional in this build and is intentionally disabled:

  • Token issuance still relies on Laravel Passport (/oauth/token, config('passport.*')), but this project runs on Laravel Sanctum and Passport is not installed.
  • The BrowserHelper class used by the JWT social login is missing from the package.

Current behaviour of the related endpoints:

  • GET /socialite-providers returns HTTP 501 (Not Implemented).
  • GET /socialite-account/{provider} aborts with HTTP 501 (Not Implemented).
  • POST /login/{provider} is registered only when social.enabled is true in config/wame-auth.php (default: false).

To restore social login: migrate token issuance to Sanctum (as in RegisterDeviceAction / LaravelAuthController::login), restore BrowserHelper, then remove the 501 guards in SocialiteProviderController and SocialiteAccountController. The Passport-based "Setup OAuth2" instructions below are legacy and are not required for the non-social auth features, which use Sanctum.

Setup

composer require wamesk/laravel-auth

Add the service provider to array of providers in config/app.php

'providers' => [
    ...
    /*
     * Third Party Service Providers...
     */
    \Wame\LaravelAuth\LaravelAuthServiceProvider::class,
];

Make sure you have \App\Models\User class. If you have it with different namespace or classname you can change it in config/wame-auth.php

'model' => 'App\\Models\\User' // Change it here when needed

Make changes to the config/auth.php file:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    
    // Add lines below
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

Make changes to the config/passport.php file:

'guard' => 'api', // Change value to 'api'

'password_grant_client' => [ // Password Grant Client - Login/Registration
    'id' => env('PASSPORT_PASSWORD_GRANT_CLIENT_ID'),
    'secret' => env('PASSPORT_PASSWORD_GRANT_CLIENT_SECRET'),
],

'personal_access_client' => [ // Personal Access Client - Social
    'id' => env('PASSPORT_PERSONAL_ACCESS_CLIENT_ID'),
    'secret' => env('PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET'),
],

Make changes in migrations database/migrations/2023_01_17_074644_create_activity_log_table.php:

$table->bigIncrements('id');
$table->string('log_name')->nullable();
$table->text('description');
$table->nullableUlidMorphs('subject', 'subject'); // <-- Change to this value
$table->nullableUlidMorphs('causer', 'causer');   // <-- Change to this value
$table->json('properties')->nullable();
$table->timestamps();
$table->index('log_name');

Optionally make changes to the config/eloquent-sortable.php file:

 'order_column_name' => 'sort_order',

Run migrations

php artisan migrate

Setup OAuth2

php artisan passport:install

Set passport output in .env file:

PASSPORT_PERSONAL_ACCESS_CLIENT_ID=<"OUTPUT-PERSONAL-CLIENT-ID">
PASSPORT_PERSONAL_ACCESS_CLIENT_SECRET=<"OUTPUT-PERSONAL-CLIENT-SECRET">

PASSPORT_PASSWORD_GRANT_CLIENT_ID=<"OUTPUT-GRANT-CLIENT-ID">
PASSPORT_PASSWORD_GRANT_CLIENT_SECRET=<"OUTPUT-GRANT-CLIENT-SECRET">

Configuration

This is the content of the file that will be published in config/wame-auth.php

<?php

use Illuminate\Validation\Rules\Password;

return [
    
    // User Model
    'model' => \Wame\LaravelAuth\Models\BaseUser::class,

    /* Login Options */
    'login' => [

        // Determine if login should be possible.
        'enabled' => true,

        // Enable this if only verified users can log in.
        'only_verified' => false,

        // Additional parameters to login request
        'additional_body_params' => [
            // Example: 'app_version' => 'required|string|min:1'
        ]
    ],

    /* Register Options */
    'register' => [

        // Determine if registration should be possible.
        'enabled' => true,

        // Enable this if verification link should be sent after successful registration.
        'email_verification' => true,

        // Determine rules for password
        'password_rules' => [
            'required',
            'string',
            Password::min(8)
                ->mixedCase()
                ->numbers()
                ->symbols()
                ->uncompromised(),
            'confirmed'
        ],

        // Additional parameters to register request
        'additional_body_params' => [
            // Example: 'app_version' => 'required|string|min:1'
        ]
    ],

    /* Email verification Options */
    'email_verification' => [

        // Determine if email verification should be enabled.
        'enabled' => true,

        // The number of minutes the verification link is valid
        'verification_link_expires_after' => 120

    ],

    /* Routing Options */
    'route' => [
        'prefix' => 'api/v1'
    ]
];

Publishing Views

php artisan vendor:publish --provider="Wame\LaravelAuth\LaravelAuthServiceProvider" --tag="views"

Publishing Translations

php artisan vendor:publish --provider="Wame\LaravelAuth\LaravelAuthServiceProvider" --tag="translations"

Modifications / Extensions

Edit/Add functions and documentation

  • create controller AuthController.php by following the example on the documentation below
class AuthController extends LaravelAuthController 
  • Copy from vendor/wamesk/laravel-auth/routes/api.php to routes/api.php
Route::controller(\App\Http\Controllers\v1\AuthController::class)->prefix('v1')->name('auth.')
    ->group(function () {

        if (config('wame-auth.register.enabled')) {
            Route::post('/register', 'register')->name('register');
        }

        if (config('wame-auth.login.enabled')) {
            Route::post('/login', 'login')->name('login');
            Route::middleware('auth:api')->post('/logout', 'logout')->name('logout');
        }

        if (config('wame-auth.email_verification.enabled')) {
            Route::post('/email/send_verification_link', 'sendVerificationLink')->name('verify.send_verification_link');
        }

        Route::post('/password/reset/send', 'sendPasswordReset')->name('password.reset.send');
        Route::post('/password/reset', 'validatePasswordReset')->name('password.reset');
        
        if (config('wame-auth.social.enabled')) {
            Route::post('/login/{provider}', 'socialLogin')->name('social-login');
        }
    });

Add documentation to function Example: app/Http/Controllers/v1/AuthController.php

class AuthController extends LaravelAuthController
{
    /*
    Here will be the documentation for register
    */
    
    public function register(Request $request): JsonResponse
    {
        return parent::register($request);
    }

Add data to login response / Edit function Example: app/Http/Controllers/v1/AuthController.php

    public function login(Request $request): JsonResponse
    {
        $return = parent::login($request);
        $data = $return->getData();

        $personal_number = User::whereId($data->data->user->id)->first()->personal_number;
        $data->data->user->personal_number = $personal_number;
        $return->setData($data);

        return $return;
    }

Example how you can add parameters to registration by using Observer:

public function handle(UserCreatingEvent $event)
    {
        $user = $event->entity;
        
        $user->team_id = request()->team_id;
        $user->approve ?: $user->approve = 0;
    }

wamesk/laravel-auth 适用场景与选型建议

wamesk/laravel-auth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 657 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 wamesk/laravel-auth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wamesk/laravel-auth 我们能提供哪些服务?
定制开发 / 二次开发

基于 wamesk/laravel-auth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-19