laravel : how to queue verification emails properly - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, July 2, 2023

laravel : how to queue verification emails properly

I'm using Laravel 10, and I've been looking for a way to queue verification emails sent by Laravel, I came across this post where they suggested overriding and extending the VerifyEmail class so we can implement ShouldQueue in the new class, but for some reason only when I implement ShouldQueue the queued job fails with this exception :

Symfony\Component\Routing\Exception\RouteNotFoundException: Route [verification.verify] not defined.

here is my code related to this matter:

Models\User.php :

    public function sendEmailVerificationNotification() {
        $this->notify(new QueuedVerifyEmail);
    }

Notifications\QueuedVerifyEmail.php (here if I don't implement ShouldQueue it'll work as normal, slow response)

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Auth\Notifications\VerifyEmail;

class QueuedVerifyEmail extends VerifyEmail implements ShouldQueue
{
    use Queueable;
}

AuthController.php

// sending verification email when registering
    public function register(StoreUserRequest $request) {
        
        $user = User::create($request->all());

        if($user) {
            $token = $user->createToken('api token of ' . $user->name);

            // sending verification mail
            $user->sendEmailVerificationNotification();

            return $this->success([
                'user' => $user,
                'token' => $token
            ], 201, "user created successfully!");
        }else {
            return $this->failed([], 500, $user);
        }
            
    }
// verifying via the sent link
    public function verifyEmail(EmailVerificationRequest $request) {

        if($request->user()->hasVerifiedEmail()) {
            return $this->success([], 208, "already verified!");
        }

        $request->fulfill();
        return $this->success([], 200, "verified!");

    }

routes\api.php

Route::prefix('v1')->group(function() {
    ...
    // email verification
    Route::get('/email/verify/{id}/{hash}', [AuthController::class, 'verifyEmail'])->middleware(['auth:sanctum', 'signed'])->name('verification.verify');
});

route list

GET|HEAD   / .................................................................. 
  POST       _ignition/execute-solution ignition.executeSolution › Spatie\Larave… 
  GET|HEAD   _ignition/health-check ignition.healthCheck › Spatie\LaravelIgnitio… 
  POST       _ignition/update-config ignition.updateConfig › Spatie\LaravelIgnit… 
  GET|HEAD   sanctum/csrf-cookie sanctum.csrf-cookie › Laravel\Sanctum › CsrfCoo… 
  GET|HEAD   v1/address .............................. v1\AddressController@index 
  POST       v1/address .............................. v1\AddressController@store 
  DELETE     v1/address/{address} .................. v1\AddressController@destroy 
  PATCH      v1/address/{address} ................... v1\AddressController@update 
  GET|HEAD   v1/admin/get-c-a ........................ v1\ClientsController@getCA 
  GET|HEAD   v1/client ............................... v1\ClientsController@index 
  POST       v1/client ............................... v1\ClientsController@store 
  PATCH      v1/client/{client} ..................... v1\ClientsController@update 
  GET|HEAD   v1/email/verify/{id}/{hash} verification.verify › v1\AuthController… 
  GET|HEAD   v1/users .................................. v1\UsersController@index 
  POST       v1/users/login ............................. v1\AuthController@login 
  GET|HEAD   v1/users/logout ........................... v1\AuthController@logout 
  GET|HEAD   v1/users/logout-all .................... v1\AuthController@logoutAll 
  POST       v1/users/register ....................... v1\AuthController@register

I'm very new to Laravel, so if you have any better way to queue the verification emails please do let me know.



source https://stackoverflow.com/questions/76592538/laravel-how-to-queue-verification-emails-properly

No comments:

Post a Comment