arhamlabs / authenticator
Requires
- arhamlabs/error-handler: 2.*
- google/apiclient: 2.13
- laravel/sanctum: *
README
此包将为用户提供用户身份验证API,包括注册和登录API,并带有路由。
安装
为了安装此包,请使用以下指定的命令 -
composer require arhamlabs/authenticator
所需包
在安装包时,您需要在PHP的ini文件中启用sodium扩展。
配置
进入 config/app.php 文件,然后在提供者中添加socialite服务
'providers' => [ .... .... Arhamlabs\Authentication\AuthenticationServiceProvider::class ],
默认配置设置在 config/al_auth_config.php 文件中。将此文件复制到您的配置目录以修改值,或者您可以使用此命令发布配置
php artisan vendor:publish --provider="Arhamlabs\Authentication\AuthenticationServiceProvider"
最后,您应该运行您的数据库迁移。此包将在数据库中创建以下表
1.temp_registrations 2.auth_settings 3.temp_otp
此外,为了进行移动OTP身份验证,还会使用一个额外的迁移。该迁移将在用户表中添加列。
命令
php artisan migrate
Laravel提供了默认迁移create_personal_access_tokens_table,用于sanctum。只需编辑或创建新的迁移以修改personal_access_tokens表,并添加以下列名“expires_at”。
$table->timestamp('expires_at')->nullable();
Sanctum Token Ability Middleware Setup
Sanctum还包括两个中间件,可以用于验证传入请求是否使用具有给定能力的令牌进行身份验证。要开始,将以下中间件添加到应用程序的 app/Http/Kernel.php 文件中的 $routeMiddleware 属性
'abilities' => \Laravel\Sanctum\Http\Middleware\CheckAbilities::class, 'ability' => \Laravel\Sanctum\Http\Middleware\CheckForAnyAbility::class
Sanctum Token authentication exception handling on route
为了处理API路由上的默认异常,例如AuthenticationException/AccessDeniedHttpException,请将以下代码添加到应用程序的 app/Exception/Handler.php 文件中的 register 函数中
$this->renderable(function (AuthenticationException $e, $request) { $errorResponse = new ApiResponse; if ($request->is('api/*')) { $customUserMessageTitle = 'Sorry, we were unable to authenticate your request'; $errorResponse->setCustomResponse($customUserMessageTitle); return $errorResponse->getResponse(401, []); } }); $this->renderable(function (AccessDeniedHttpException $e, $request) { $errorResponse = new ApiResponse; if ($request->is('api/*')) { return $errorResponse->getResponse(403, []); } }); $this->renderable(function (NotFoundHttpException $e, $request) { $errorResponse = new ApiResponse; if ($request->is('api/*')) { return $errorResponse->getResponse(404, []); } });
更新默认 App\Models\User.php
use Arhamlabs\Authentication\Models\AuthSetting; protected $fillable = [ 'uuid', 'name', 'sso_type', 'first_name', 'last_name', 'username', 'email', 'mobile', 'country_code', 'password', 'email_verified_at', ]; public function settings() { return $this->hasOne(AuthSetting::class, 'model_id', 'id')->where('model_name', 'App\Models\User'); }
API参考
注册
用户注册
- 用户可以使用电子邮件和密码进行注册。数据将保存在临时表中。
- 如果配置标志email_verification=false,则详情将保存在主表中,即users表以及user_setting表也将更新。
- 如果配置设置为email_verification=true,则将向用户发送验证邮件。
POST /api/package/auth/register
用户使用手机和OTP进行注册
- 用户可以使用手机和OTP进行注册。OTP将通过短信发送给用户。
- 一旦发送OTP,temp_otp表将用于维护验证详情。
- 如果手机号已注册,则不允许用户使用相同的手机号进行注册。
POST /api/package/auth/mobile-register
移动OTP验证
- 此API用于验证OTP。
- 一旦OTP验证成功,将生成一个sanctum令牌。
- 如果手机号已注册,则不允许用户使用相同的手机号进行注册。
POST /api/package/auth/mobile-register-verify-otp
用户登录
使用用户名和密码进行用户登录
- 用户可以使用电子邮件/手机/用户名和密码进行登录。
- 一旦用户通过身份验证,则将生成laravel sanctum令牌。
POST /api/package/auth/login
使用电子邮件和OTP进行用户登录
- 用户可以使用电子邮件和OTP进行登录。OTP将通过电子邮件发送给用户。
- 一旦发送OTP,temp_otp表将用于维护验证详情。
POST /api/package/auth/sent-email-otp
邮件OTP验证
- 此API用于验证OTP。
- 一旦OTP验证成功,将生成一个令牌。将添加尝试次数到函数中。
POST /api/package/auth/mail-verify-otp
使用同一端点使用手机和OTP进行用户注册/登录
- 用户可以使用短信和OTP进行登录。OTP将通过短信发送给用户。
- 一旦发送OTP,temp_otp表将用于维护验证详情。
- 如果手机号已注册,则允许用户登录
- 如果手机号未注册,则允许用户注册然后登录。
POST /api/package/auth/sent-mobile-otp
移动OTP验证
- 此API用于验证OTP。
- 一旦OTP验证成功,将生成一个令牌。将添加尝试次数到函数中。
- 在配置文件中,如果 allow_login_or_registration_through_mobile_number 标志设置为 true,则将应用以下场景。
- 如果手机号已注册,则允许用户登录
- 如果手机号未注册,则允许用户注册然后登录。
POST /api/package/auth/sms-verify-otp
忘记密码
- 用户可以通过电子邮件重置密码。重置链接将通过电子邮件发送给用户。用户可以通过重置链接更改密码。
- 对于被阻止的用户,包不会生成重置链接。
POST /api/package/auth/forgot-password
登出
- 此 API 用于登出用户,并将销毁该用户的 sanctum 令牌。
POST /api/package/auth/logout
设置/更改密码
- 用户可以使用 sanctum 令牌设置/更改密码。如果用户通过 sso 或 otp 登录,则用户可以首次设置密码。下一次更改密码时,用户必须提供当前密码。
POST /api/package/auth/update-password
社交媒体登录/注册
- 用户可以使用 id 令牌通过 Google 账户登录。对于 Google 账户验证,包将使用 Google 客户端 包验证 id 令牌和 aud(客户端 ID)。
POST /api/package/auth/sso-login
Linkedin-移动端
- 用户可以使用 id 令牌通过 Linkedin 账户登录。包将验证 id 令牌。
- sso_type 必须为 'linkedin-mobile'
POST /api/package/auth/sso-login
Linkedin-网页端
- 用户可以使用授权代码通过 Linkedin 账户登录。包将验证授权代码,并从 LinkedIn 服务器获取访问令牌。
- 包将通过从 LinkedIn 服务器获取详细信息来更新用户信息。
- sso_type 必须为 'linkedin-web'
- idToken 必须是授权代码。
POST /api/package/auth/sso-login
Apple
- 用户可以使用 id 令牌通过 Apple 账户登录。对于 LinkedIn 账户验证,包将验证 id 令牌。
POST /api/package/auth/sso-login
包配置文件
al_auth_config.php
- 开发者可以使用 al_auth_config.php 文件更改配置,如下所示。
//check email verification requirement 'email_verification' => true, //check mobile verification requirement 'mobile_verification' => true, /* If this flag is set true then user can login or register using the same endpoint based on the below scenario 1. If the user is registered initially and the api is fired then he will be logged in. 2. If the user is not registered initially and the api is fired then he will be registered. */ 'allow_login_or_registration_through_mobile_number' => false, //length for otp 'otp_length' => 4, //otp expire in minutes 'otp_expire' => 5, //SMS OTP Configuration 'sms' => [ 'delay' => 60, //in seconds 'per_day_count' => 5 //per day sms limit for user ], //allow multi login with same credentials 'user_multi_login' => true, //default user type 'user_Type' => 'app_user', //if true then it will check user block status 'is_check_user_block' => true, //email verification mail expiry in hours 'email_verification_mail_expiry' => 48, //forgot password mail expiry in hours 'forgot_password_mail_expiry' => 48, //email link encryption key 'email_encryption_key' => env('EMAIL_ENCRYPTION_KEY', 'ALAUTH'), //social media login linkedin config setup 'linkedin' => [ 'LINKEDIN_REDIRECT_URI' => env('LINKEDIN_REDIRECT_URI'), 'LINKEDIN_CLIENT_ID' => env('LINKEDIN_CLIENT_ID'), 'LINKEDIN_CLIENT_SECRET' => env('LINKEDIN_CLIENT_SECRET'), 'CURLOPT_SSL_VERIFYPEER' => env('CURLOPT_SSL_VERIFYPEER') ], //social media login apple config setup 'apple' => [ 'TOKEN_ISS' => env('TOKEN_ISS', "https://appleid.apple.com"), 'TOKEN_AUD' => env('TOKEN_AUD', "com.example.co.app"), ]