承接 think.studio/laravel-email-change-verification 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

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

统计信息

  • 总下载量: 63
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-26