Laravel email verify using passport and without using auth middleware and EmailVerificationRequest

Here’s the updated version with the point about API and client-side email verification:



---



🚀 **Enhancing User Engagement with Email Verification in Laravel 11** 🚀



Implementing email verification is a crucial step in ensuring a secure and trustworthy user experience. Here’s how you can seamlessly integrate it into your Laravel 11 application:



### 1. **Implement the MustVerifyEmail Interface**



Start by enhancing your `User` model to implement the `MustVerifyEmail` interface:



```php

<?php



namespace App\Models;



use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Illuminate\Notifications\Notifiable;



class User extends Authenticatable implements MustVerifyEmail

{

    use Notifiable;



    // ...

}

```



### 2. **Trigger Email Verification on Registration**



Use the `Registered` event to automatically send verification emails when users register:



```php

public function verifyEmail(Request $request)

{

    try {

        $user = $request->user('api');

        event(new Registered($user));

    } catch (\Exception $e) {

        // Handle any exceptions

    }

}

```



### 3. **Create a Dedicated Verification Route**



Define a route to handle email verification with the name `verification.verify`:



```php

Route::get('/email/verify/{id}/{hash}', [VerificationController::class, 'verify'])

    ->middleware(['signed'])

    ->name('verification.verify');

```



### 4. **Build the Verification Controller**



Handle the email verification logic within the `VerificationController`:



```php

<?php



namespace App\Http\Controllers;



use App\Models\User;

use Illuminate\Auth\Events\Verified;

use Illuminate\Foundation\Auth\EmailVerificationRequest;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Auth;



class VerificationController extends Controller

{

    public function verify(Request $request, $id, $hash)

    {

        // Retrieve the user by ID

        $user = User::findOrFail($id);



        // Validate the email hash

        if (!hash_equals((string) $hash, sha1($user->getEmailForVerification()))) {

            return response()->json(['message' => 'Invalid verification link.'], 400);

        }



        // Mark the email as verified

        if ($user->markEmailAsVerified()) {

            event(new Verified($user));

        }



        return view('others.verified-after-email');

    }

}

```



### 5. **No Authenticated Middleware Required**



This approach allows you to handle email verification **without requiring authenticated middleware**. Users can verify their emails even if they are not logged in, simplifying the onboarding process and enhancing user experience.



### 6. **API and Client-Side Email Verification**



This method is suitable for both **API and client-side email verification**. It allows you to create a seamless experience across different platforms, making it easier for users to verify their emails regardless of how they access your application.



### 🔑 **Why Email Verification?**



✅ **Boosts Security**: Ensures that users own their email addresses.



✅ **Enhances Trust**: Builds a trustworthy relationship with your users.



✅ **Reduces Spam**: Helps keep your platform clean and engaged.



💬 Have you implemented email verification in your applications? Share your experiences below! 



#Laravel #WebDevelopment #EmailVerification #Programming #SoftwareEngineering



---



Let me know if you need any further tweaks!

Comments