think.studio/laravel-email-change-verification
最新稳定版本:1.2.0
Composer 安装命令:
composer require think.studio/laravel-email-change-verification
包简介
Package allow add verification for new email when user change email
README 文档
README
Package allow to add verification for new email when user change email
Installation
You can install the package via composer:
composer require think.studio/laravel-email-change-verification php artisan vendor:publish --provider="EmailChangeVerification\ServiceProvider" --tag="config"
Configuration and usage
- Create migration. Package not provide default migrations, so you will need create table manually. But packages provide class with default columns.
public function up() { Schema::create('email_changes', function (\Illuminate\Database\Schema\Blueprint $table) { \EmailChangeVerification\Database\MigrationHelper::defaultColumns($table); }); } public function down() { Schema::dropIfExists('email_changes'); }
php artisan migrate
- Update User model
use EmailChangeVerification\User\HasEmailChangeVerification; use EmailChangeVerification\User\WithEmailChangeVerification; class User extends Authenticatable implements HasEmailChangeVerification { use WithEmailChangeVerification; // ... }
- Send verification on email change
if ($user->email != $request->input('email')) { $status = EmailChange::sendVerificationLink([ 'email' => $user->email, ], $request->input('email')); if ($status == EmailChange::VERIFICATION_LINK_SENT) { $successMessage = __($status); } else { throw ValidationException::withMessages([ 'email' => __($status), ]); } }
- Verify new email
// routes Route::get( '/email-change-verification/{token}', [ \App\Http\Controllers\Dashboard\ProfileController::class, 'verifyNewEmail', ] )->name('email.change.verification');
// controller public function verifyNewEmail( Request $request, string $token ) { $validator = Validator::make( array_merge($request->all(), [ 'token' => $token, ]), [ 'email' => [ 'required', 'email' ], 'new_email' => [ 'required', 'email' ], 'token' => [ 'required', 'string', 'max:64' ], ] ); if($validator->fails()) { abort(404); } $status = EmailChange::verify( [ 'email' => $request->input( 'email', '' ), 'new_email' => $request->input( 'new_email', '' ), 'token' => $token, ], function ( $user, string $newEmail ) { // user manipulation $user->email = $newEmail; $user->save(); } ); if ( $status != EmailChange::EMAIL_CHANGED ) { return __( $status ); // return view or redirect } return 'Success'; // return view or redirect }
- Check is request sent
// returns email or null if expired, Example: test@test.com $lastRequestedEmailChange = EmailChange::getRepository()->lastRequestedEmail($user);
Credits
统计信息
- 总下载量: 62
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-08-26