1

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);
    }
}
2
  • 2
    Never share screenshots of your code. That is text, and it should be shared as text. Also, please share your attempts to resolve the problem Commented Feb 22, 2022 at 13:07
  • The answer below by Wael Khalifa was exactly the solution I was going for. For the future I will indeed share in code, instead of images. I will update my question with the answer implemented. Commented Feb 22, 2022 at 14:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

That was exactly it. I was staring myself completely blind! Thank you!
Your always welcome 😊

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.