laravel / fortify

Backend controllers and scaffolding for Laravel authentication.

Home Page:https://laravel.com/docs/fortify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Change response options after email verification

ErwinLiemburg opened this issue · comments

Hi,

the current VerifyEmailController only redirects to a page if user is already accepted or when verified. Issuing a verification from an API request does not respond as expected.

Although setting 'views' to false, email verification is still redirecting to a page instead of an API response. Although redirecting to a page would be an option, but responding with an API/json result would be prefered.

I've done some tinkering, and based on the EmailVerificationNotificationController I came up with the following changes to Laravel\Fortify\Http\Controllers\VerifyEmailController:

class VerifyEmailController extends Controller
{
    /**
     * Mark the authenticated user's email address as verified.
     *
     * @param  \Laravel\Fortify\Http\Requests\VerifyEmailRequest  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function __invoke(VerifyEmailRequest $request)
    {
        if ($request->user()->hasVerifiedEmail()) {
            return $request->wantsJson()
                ? new JsonResponse(['message' => 'Your email is already verified.'], 422)
                : redirect()->intended(config('fortify.home').'?verified=1');
        }

        if ($request->user()->markEmailAsVerified()) {
            event(new Verified($request->user()));
        }

        return $request->wantsJson()
            ? new JsonResponse(['message' => 'Your email has been verified.'], 202)
            : redirect()->intended(config('fortify.home').'?verified=1');
    }
}

This works for me now, but I also understand that changing the vendor code is not the best option. (I tried to add an own controller, but that would also mean that I had to create another route, which made things super complicated.)

Is it possible to change the VerifyEmailController class to support API/json response?

Best regards,

Erwin

Feel free to attempt a PR 👍