yaroslawww / laravel-email-change-verification
This package is abandoned and no longer maintained.
The author suggests using the think.studio/laravel-email-change-verification package instead.
Package allow add verification for new email when user change email
1.2.0
2023-08-26 08:14 UTC
Requires
- php: ^8.0
- illuminate/support: ^9.0|^10.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.20
- orchestra/testbench: ^8.6
- phpunit/phpunit: ^10.3
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.14
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);