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

This package is auto-updated.

Last update: 2023-08-26 08:32:18 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

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

  1. 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
  1. Update User model
use EmailChangeVerification\User\HasEmailChangeVerification;
use EmailChangeVerification\User\WithEmailChangeVerification;

class User extends Authenticatable implements HasEmailChangeVerification
{
    use WithEmailChangeVerification;
    // ...
}
  1. 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),
        ]);
    }
}
  1. 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
}
  1. Check is request sent
// returns email or null if expired, Example: test@test.com
$lastRequestedEmailChange = EmailChange::getRepository()->lastRequestedEmail($user); 

Credits

  • Think Studio