Laravel 2 دقيقة للقراءة 488 مشاهدات

كيفية تطبيق التحقق من البريد الإلكتروني في Laravel 11

أعدّ التحقق من البريد الإلكتروني لتسجيلات المستخدمين الجدد مع رسائل مخصصة ووظيفة إعادة الإرسال.

Email verification

الخطوة 1: تطبيق MustVerifyEmail

use Illuminate\Contracts\Auth\MustVerifyEmail;

class User extends Authenticatable implements MustVerifyEmail
{
    // ...
}

الخطوة 2: إضافة المسارات

use Illuminate\Foundation\Auth\EmailVerificationRequest;

// Show verification notice
Route::get('/email/verify', function () {
    return view('auth.verify-email');
})->middleware('auth')->name('verification.notice');

// Handle verification link
Route::get('/email/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
    return redirect('/dashboard')->with('verified', true);
})->middleware(['auth', 'signed'])->name('verification.verify');

// Resend verification email
Route::post('/email/verification-notification', function (Request $request) {
    $request->user()->sendEmailVerificationNotification();
    return back()->with('message', 'Verification link sent!');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');

الخطوة 3: حماية المسارات

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index']);
});

الخطوة 4: واجهة إشعار التحقّق

<div class="alert">
    <p>Please verify your email address.</p>

    @if (session('message'))
        <p class="success">{{ session('message') }}</p>
    @endif

    <form method="POST" action="{{ route('verification.send') }}">
        @csrf
        <button type="submit">Resend Verification Email</button>
    </form>
</div>

الخطوة 5: بريد تحقّق مخصّص

// app/Notifications/CustomVerifyEmail.php
class CustomVerifyEmail extends Notification
{
    public function toMail($notifiable)
    {
        $url = URL::temporarySignedRoute(
            'verification.verify',
            now()->addMinutes(60),
            ['id' => $notifiable->id, 'hash' => sha1($notifiable->email)]
        );

        return (new MailMessage)
            ->subject('Verify Your Email - ' . config('app.name'))
            ->greeting('Hello ' . $notifiable->name . '!')
            ->line('Click the button below to verify your email.')
            ->action('Verify Email', $url)
            ->line('This link expires in 60 minutes.');
    }
}

// In User model
public function sendEmailVerificationNotification()
{
    $this->notify(new CustomVerifyEmail);
}
مشاركة هذه المقالة:
ES
كتبه

Edrees Salih

مهندس برمجيات متكامل يتمتع بخبرة 9 سنوات. شغوف ببناء حلول قابلة للتطوير ومشاركة المعرفة مع مجتمع المطورين.

عرض الملف الشخصي

التعليقات (0)

اترك تعليقًا

لن يتم نشر بريدك الإلكتروني.

لا توجد تعليقات بعد. كن أول من يشارك أفكاره!

مقالات ذات صلة

مقالات ذات صلة

هل تحتاج مساعدة في مشروعك؟

احجز استشارة مجانية لمدة 30 دقيقة لمناقشة تحدياتك التقنية واستكشاف الحلول معًا.