Home > Enterprise >  Laravel E-mail verification (re-send method)
Laravel E-mail verification (re-send method)

Time:02-22

TLDR: I am new to Laravel but I am trying to re-send a verify e-mail to an e-mail adress through a request->input('email').

Case:

[Working] When a user registrates I send out an e-mail to the registrated user through Laravels default e-mail verification scaffolding. Before the user can use the platform it needs to be verified. The user get's an e-mail and can click on the button in the e-mail to verify it's account.

[Not working] But if a user for some reason lost his/her verification e-mail I want to re-send it through an API call while checking (but not exposing) if the e-mail filled in, is available. This is not supported by default in Laravels e-mail verification scaffolding.

What have I done:

  • Added in the user model: implements MustVerifyEmail which uses use Illuminate\Contracts\Auth\MustVerifyEmail;

  • In the default VerificationController I changed: $this->middleware('auth') to $this->middleware('auth')->except(['resend']);

  • In api.php added two routes to a custom VerifyEmailController class

    • Route::group(['middleware' => ['signed', 'throttle:6,1']], function () { Route::get('/email/verify/{id}/{hash}', [VerifyEmailController::class, '__invoke'])->name('verification.verify'); }); [This works]

    • Route::group(['middleware' => ['throttle:6,1']], function () { Route::post('/email/verify/resend', [VerifyEmailController::class, 'resend'])->name('verification.resend');}); [Call works but resend method logic is wrong]

  • And this is my VerifyEmailController class resend method VerifyEmailController resend method

Ofcourse this is not how it's done, but I can't think of a way to make this work due to my lack of knowledge and experience.

Right now I do get a json response back that it has send the e-mail, but obviously it has not fired the Verify event to actually send the verification e-mail in the request.

Json response that e-mail has been send to e-mail request

If you want more screenshots of code let me know.

Solution from Wael Khalifa :

in api.php I declared the following route

Route::group(['middleware' => ['throttle:6,1']], function () {
Route::post('/email/verify/resend', [VerifyEmailController::class, 'resend'])->name('verification.resend');});

in the VerifyEmailController class, declared the following method

class VerifyEmailController extends Controller
{

    public function resend(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|string|email|max:45',
        ]);
        
        // These two lines below where the solution to my problem.
        $user = User::where('email', $request->input('email'))->first();
        $user->sendEmailVerificationNotification();

        return Response::json(["status" => 'Verification e-mail send.', "email" => $request->input('email')], 201);
    }
}

CodePudding user response:

Get user from your database after that use sendEmailVerificationNotification() method to resend the verification email

 Route::post('resend/verification-email', function (\Illuminate\Http\Request $request) {
$user = User::where('email',$request->input('email'))->first();

    $user->sendEmailVerificationNotification();

    return 'your response';
})->middleware('throttle:6,1');

You can do the same thing in your controller if you need help with that add a comment to me

Hope the answer help you

  • Related