非法 / insideauth
Laravel InsideAuth 是一款针对 Laravel 扩展包的无缝侧边认证解决方案。
Requires
- php: ^8.1
- illegal/laravel-utils: ^1.0
- laravel/framework: ^9|^10
- laravel/sanctum: ^3.0
Requires (Dev)
- fakerphp/faker: ^1.9.1
- laravel/pint: ^1.0
- mockery/mockery: ^1.5
- nunomaduro/collision: ^7.0
- nunomaduro/larastan: ^2.0
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- pestphp/pest-plugin-mock: ^2.0
- phpunit/phpunit: ^10.0
- spatie/laravel-ignition: ^2.0
- spatie/laravel-ray: ^1.32
README
该仓库正在积极开发中,目前不适合用于生产。
Laravel InsideAuth 是一款针对 Laravel 扩展包的无缝侧边认证解决方案。它提供了一个简单且独立的认证机制,让开发者能够专注于构建其扩展包或应用程序的核心功能,无需担心管理独立的认证系统。
特性
- 易于集成:只需几行代码即可快速将 InsideAuth 集成到您现有的 Laravel 扩展包或应用程序中。
- 独立认证:InsideAuth 为您的 Laravel 扩展包创建了一个独立的认证系统,确保它不会干扰您主要应用程序的认证。
- 可定制:通过各种配置选项来定制登录、注册和密码重置过程,以满足您的特定需求。
- 安全:InsideAuth 遵循行业标准的安全实践,并使用最新的加密方法来保护您的应用程序免受未经授权的访问。
- 中间件支持:通过中间件支持,轻松根据用户的认证状态限制对特定路由的访问。
目录
安装
您可以使用 Composer 无缝安装此包。
composer require illegal/insideauth
安装完成后,必须迁移数据库表,以便为该包设置默认表。
InsideAuth 为表使用前缀,可根据需要自定义。有关详细信息,请参阅配置部分。
php artisan migrate
配置
默认情况下,InsideAuth 将 auth_
前缀应用于数据库表。要自定义此前缀,只需将 INSIDEAUTH_TABLE_PREFIX
变量添加到您的 .env
文件中。
INSIDE_AUTH_DB_PREFIX="myprefix_"
使用
您可以通过使用 InsideAuth 门面对象来创建一个新的认证集,该门面对象返回 Authenticator 类的实例。此类包含认证集的所有配置设置。
通常建议在服务提供者的 boot
方法中注册认证集。
InsideAuth::boot('myproject');
InsideAuth 会自动注册认证集的路由。
路由以认证集的名称为前缀;在本例中,它们将以前缀 myproject
开头。
/myproject/login
/myproject/register
...
您可以通过使用 Authenticator 类的参数来获取所有功能的路由。
有关详细信息,请参阅参数部分。
中间件
InsideAuth 随附一系列中间件。
这些中间件允许您根据用户的认证状态控制对特定路由的访问。
您可以通过使用 Authenticator 类的参数来获取所有功能的中间件。
有关更多信息,请参阅参数部分。
定制
在注册新的认证集后,您可以使用 Authenticator 类上提供的函数来根据您的需求定制认证过程。
以下是可以用于定制认证过程的所有函数列表
\Illegal\InsideAuth\InsideAuth::boot('myproject') // Disable Registration. You can pass a boolean value. This will disable registration if the value is true. ->withoutRegistration() // Disable Forgot Password. You can pass a boolean value. This will disable forgot password if the value is true. ->withoutForgotPassword() // Disable Email Verification. You can pass a boolean value. This will disable email verification if the value is true. ->withoutEmailVerification() // Disable User Profile. You can pass a boolean value. This will disable user profile if the value is true. ->withoutUserProfile() // This will set the dashboard route, the user will be redirected to after login. ->withDashboard('my_dashboard_route') // The template for the confirm password page. ->withConfirmPasswordTemplate('my_confirm_password_template') // The template for the forgot password page. ->withForgotPasswordTemplate('my_forgot_password_template') // The template for the login page. ->withLoginTemplate('my_login_template') // The template for the register page. ->withRegisterTemplate('my_register_template') // The template for the reset password page. ->withResetPasswordTemplate('my_reset_password_template') // The template for the verify email page. ->withVerifyEmailTemplate('my_verify_email_template') // The template for the user profile page. ->withProfileEditTemplate('my_profile_edit_template');
参数
通常,您需要在应用程序的生命周期中访问用于身份验证的路由和中件。
有几种方法可以访问当前的认证器实例。
请求特定的认证器实例
$auth = InsideAuth::getAuthenticator('myproject');
myproject
是您在启动方法中提供的认证集名称。
由于当前认证器是通过中件进行注入的,这是在请求开始之前访问当前认证器的唯一方法。例如在 Service Provider 中。
这是您将用于保护路由的典型方法。
Route::middleware([ InsideAuth::getAuthenticator('myproject')->middleware_web, InsideAuth::getAuthenticator('myproject')->middleware_verified, ])->group(function () { // ... });
有关更多详细信息,请参阅安全部分。
通过 InsideAuth 面板
$auth = InsideAuth::current();
通过在 Request
属性中的注入
$auth = $request->attributes->get('authenticator');
通过提供的辅助函数
$auth = insideauth();
在您的 blade 模板中访问当前认证器实例最简单、推荐的方法是使用辅助函数。
参数完整列表
// Flags $auth->registration_enabled // Whether registration is enabled $auth->forgot_password_enabled // Whether forgot password is enabled $auth->email_verification_enabled // Whether email verification is enabled $auth->user_profile_enabled // Whether the user profile is enabled // Templates $auth->template_confirm_password // The name of the confirm password template $auth->template_forgot_password // The name of the forgot password template $auth->template_login // The name of the login template $auth->template_register // The name of the register template $auth->template_reset_password // The name of the reset password template $auth->template_verify_email // The name of the verify email template $auth->template_profile_edit // The name of the profile edit template // Routes $auth->dashboard // The name of the dashboard route $auth->route_login // The name of the login route $auth->route_register // The name of the register route $auth->route_password_request // The name of the password request route $auth->route_password_email // The name of the password email route $auth->route_password_reset // The name of the password reset route $auth->route_password_store // The name of the password store route $auth->route_logout // The name of the logout route $auth->route_verification_notice // The name of the verification notice route $auth->route_verification_verify // The name of the verification verify route $auth->route_verification_send // The name of the verification send route $auth->route_password_confirm // The name of the password confirm route $auth->route_password_update // The name of the password update route $auth->route_profile_edit // The name of the profile edit route $auth->route_profile_update // The name of the profile update route $auth->route_profile_destroy // The name of the profile destroy route // Middlewares $auth->middleware_verified // The name of the main middleware $auth->middleware_guest // The name of the guest middleware $auth->middleware_logged_in // The name of the logged in middleware $auth->middleware_web // The name of the web middleware // Security $auth->security_guard // The name of the guard $auth->security_provider // The name of the provider $auth->security_password_broker // The name of the password broker
安全
要保护您的路由,只需应用 InsideAuth 提供的适当中间件。
Route::middleware(Illegal\InsideAuth\InsideAuth::getAuthenticator('myproject')->middleware_verified)->group(function () { Route::get('/dashboard', function () { return view('dashboard'); }); });
您的路由使用标准 web
中间件至关重要。
如果您尚未包含此中间件,可以使用 InsideAuth 提供的中间件。
Route::middleware([ \Illegal\InsideAuth\InsideAuth::getAuthenticator('myproject')->middleware_web, \Illegal\InsideAuth\InsideAuth::getAuthenticator('myproject')->middleware_verified, ])->group(function () { Route::get('/dashboard', function () { return view('dashboard'); }); });
许可证
Laravel InsideAuth 是一个开源软件,许可协议为MIT 许可证。