codebar-ag/laravel-auth

这是我的包 laravel-auth

v3.0 2024-02-09 10:04 UTC

This package is auto-updated.

Last update: 2024-09-25 10:45:05 UTC


README

Latest Version on Packagist GitHub-Tests GitHub Code Style Total Downloads

此包旨在帮助您快速开始 Laravel 的身份验证。它具有自己的观点,并使用以下包:

💡 什么是 Laravel Auth?

Laravel Auth 是 Laravel Nova 内置身份验证的替代品,以获得对 Laravel Nova 的授权的更多控制。

🛠 要求

> = v1.0

  • PHP: ^8.2
  • Laravel: ^10.*
  • Microsoft SSO

⚙️ 安装

您可以通过 composer 安装此包

composer require codebar-ag/laravel-auth

将以下脚本添加到您的 composer.json 文件中

"scripts": {
    "post-update-cmd": [
      "@php artisan vendor:publish --tag=auth-assets --ansi --force"
    ],
}

将配置添加到您的 config/services.php 文件中

'microsoft' => [
    'client_id' => env('MICROSOFT_CLIENT_ID'),
    'client_secret' => env('MICROSOFT_CLIENT_SECRET'),
    'redirect' => env('MICROSOFT_REDIRECT_URI'),
    'tenant' => env('MICROSOFT_TENANT_ID'),
    'include_tenant_info' => true,
],

将以下环境变量添加到您的 .env 文件中

MICROSOFT_CLIENT_ID=your-client-id
MICROSOFT_CLIENT_SECRET=your-client-secret
MICROSOFT_REDIRECT_URI="${APP_URL}/auth/service/microsoft/redirect"
MICROSOFT_TENANT_ID=your-tenant-id

⚠️ 您需要为 MICROSOFT_REDIRECT_URI 环境变量提供一个公开可访问的 URL。您可以使用 exposengrok 进行本地开发。

APP_URL=https://your-expose-or-ngrok-url.com

# ✅ This is recommended for production as well:
MICROSOFT_REDIRECT_URI="${APP_URL}/auth/service/microsoft/redirect"

将以下特质添加到您的 User 模型中

use CodebarAg\LaravelAuth\Traits\HasAuthProviders;

更新您的 App\Http\Middleware\Authenticate 中间件

...
protected function redirectTo(Request $request): ?string
{
-    return $request->expectsJson() ? null : route('login');
+   return $request->expectsJson() ? null : route('auth.login');
}

最后,运行以下命令

php artisan auth:install

💉 测试

如果您想添加 pest 测试,请运行以下命令

php artisan auth:install-tests

接下来,将以下内容添加到您的 phpunit.xml 文件中

<testsuite name="Auth">
    <directory>tests/Auth</directory>
</testsuite>

您还需要将 Auth 添加到您的 Pest.php 文件中

uses(Tests\TestCase::class)->in('Feature', 'Auth');

🚏 路由

以下是此包提供的以下路由

🪐 Nova 调整

将用户菜单(注销)添加到您的 NovaServiceProvider 的 boot 方法中

use Illuminate\Http\Request;
use Laravel\Nova\Menu\Menu;
use Laravel\Nova\Menu\MenuItem;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        Nova::userMenu(function (Request $request, Menu $menu) {
            return $menu
                ->append(MenuItem::externalLink('Logout', route('auth.logout')));
        });

接下来,在您的 nova.php 配置中添加以下内容

/*
|--------------------------------------------------------------------------
| Nova Routes
|--------------------------------------------------------------------------
|
| These are the routes that are used by Nova to authenticate users.
|
*/

'routes' => [
    'login' => 'auth/login',
],

接下来,在您的 NovaServiceProvider 中用以下内容替换 routes 方法

注意:您不能为 ->withAuthenticationRoutes()->withPasswordResetRoutes() 注册路由,因为这会覆盖我们在 nova.php 配置中做的更改。

    /**
     * Register the Nova routes.
     *
     * @return void
     */
    protected function routes()
    {
-        Nova::routes()
-            ->withAuthenticationRoutes()
-            ->withPasswordResetRoutes();
+        Nova::routes();
    }

🔧 配置文件

您可以使用以下命令发布配置文件

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

这是发布配置文件的内容

<?php

// config for CodebarAg/LaravelAuth

use CodebarAg\LaravelAuth\Enums\ProviderEnum;

return [
    /*
    |--------------------------------------------------------------------------
    | Redirect Settings
    |--------------------------------------------------------------------------
    | You may like to define a different route once the user is
    | logged in or out. If no redirects are defined, the package will redirect to the
    | intended route. (This is the normal Laravel behaviour)
    |
    | Use the route name as defined in your routes file.
    |
    | If password-reset is not defined, the package will redirect to the login redirect route.
    |
    */
    'redirect' => [
        // 'login' => 'dashboard',
        // 'logout' => '',
        // 'password-reset' => '',
    ],
    
    /*
    |--------------------------------------------------------------------------
    | Logo Settings
    |--------------------------------------------------------------------------
    | You may like to define a different logo for the login page.
    | You can pass either a path relative to the public folder or a full url.
    |
    */
    'logo' => [
        'path' => 'vendor/auth/images/lock.svg',
        // 'path' => 'https://example.test/images/logo.png',
        'width' => '25%',
    ],

    /*
    |--------------------------------------------------------------------------
    | Middleware Settings
    |--------------------------------------------------------------------------
    | By default, the package will use the web middleware group.
    | You may define them the same way you would in your routes file.
    |
    */
    'middleware' => [
        //
    ],

    /*
    |--------------------------------------------------------------------------
    | Link  Settings
    |--------------------------------------------------------------------------
    | By default, the package will use 60 minutes as the expiration time for
    | the signed links used in the email verification process.
    | You may define a different value here.
    |
    */
    'link_expiration_in_minutes' => 60,

    /*
    |--------------------------------------------------------------------------
    | Toast Settings
    |--------------------------------------------------------------------------
    | By default, the package will use 5000 milliseconds as the time to fade
    | out the toast messages.
    | You may define a different value here.
    |
    */
    'toast_fade_time_in_milliseconds' => 5000,
    
    /*
    |--------------------------------------------------------------------------
    | Password Reset Settings
    |--------------------------------------------------------------------------
    | By default, the package will use the password_resets table.
    | You may define a different table name here.
    |
    */
    'password_reset_table' => 'password_resets',
    
    /*
    |--------------------------------------------------------------------------
    | Provider Settings
    |--------------------------------------------------------------------------
    | Add the providers you want to use here.
    | e.g ProviderEnum::MICROSOFT_OFFICE_365,
    |
    */
    'providers' => [
         ProviderEnum::MICROSOFT_OFFICE_365,
    ],
    
    
    /*
    |--------------------------------------------------------------------------
    | Feature Settings
    |--------------------------------------------------------------------------
    | By default, all features are enabled.
    | You may disable a provider by adding changing the value to false.
    |
    */
    'features' => [
        'basic' => true,
        'sso' => true,
        'password_reset' => true,
        'email_verification' => true,
    ],
];

🔐 验证

如果您想使用电子邮件验证,可以将以下内容添加到您的 User 模型中

use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail

然后您可以使用以下中间件来保护您的路由

Illuminate\Auth\Middleware\EnsureEmailIsVerified::redirectTo('auth.verification.notice'),

在 nova 中使用验证,将中间件添加到您的 nova.php 配置中

/*
|--------------------------------------------------------------------------
| Nova Route Middleware
|--------------------------------------------------------------------------
|
| These middleware will be assigned to every Nova route, giving you the
| chance to add your own middleware to this stack or override any of
| the existing middleware. Or, you can just stick with this stack.
|
*/

'middleware' => [
    'web',
    EnsureEmailIsVerified::redirectTo('auth.verification.notice'),
    HandleInertiaRequests::class,
    DispatchServingNovaEvent::class,
    BootTools::class,
],

🎨 定制

您可以使用以下命令发布视图

php artisan vendor:publish --tag=auth-views

您可以使用以下命令发布资产

php artisan vendor:publish --tag=auth-assets

您可以使用以下命令发布配置

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

您可以使用以下命令发布迁移

php artisan vendor:publish --tag=auth-migrations

您可以使用以下命令发布翻译

php artisan vendor:publish --tag=auth-translations

此包使用 Laravel Honeypot 来防止垃圾邮件。请查看 文档 了解如何自定义它。

🚧 测试

复制您的自己的 phpunit.xml 文件。

cp phpunit.xml.dist phpunit.xml

运行测试

./vendor/bin/pest

📝 更新日志

请参阅 更新日志 了解最近更改的信息。

✏️ 贡献

请参阅 贡献 以获取详细信息。

🧑‍💻 安全漏洞

请审查我们的安全策略,了解如何报告安全漏洞。

🙏 致谢

🎭 许可证

MIT许可证(MIT)。更多信息请参阅许可证文件