Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elo's avatar
Level 3

Implementing API Email Verification

I was following a post on medium on how to do API email verification in Laravel 5.8 here https://medium.com/@pran.81/how-to-implement-laravels-must-verify-email-feature-in-the-api-registration-b531608ecb99

I tested it and it works perfectly but I was wondering how it would work when a web or mobile application consumes the API. Here are my thoughts

  1. Web or mobile user registers a new account by calling the route Route::post('register', 'AuthController@register') and the controller register method is this
public function register(Request $request)
{
    // validate inputs

    // store new user & send verify email notification to user
    $user = User::create([
        'firstname' => $request->firstname,
        'lastname'  => $request->lastname,
        'username'  => $request->username,
        'password'  => bcrypt($request->password)
    ]);

    $user->sendApiEmailVerificationNotification();

    // assign access token to newly registered user
    
    // return access token & user data
    
}
  1. User get an email with a Verify Email Address button

Now what should happen next? Should the user be redirected to the web app login page? If yes, how do I customise the redirect url? At the moment when I click on the verify email address button, a browser opens up and I get all the response on the browser page like this api endpoint response

0 likes
4 replies
aurawindsurfing's avatar

Hey @elo

Email veryfication should do only what the name says - verify email. User gets email with unique URI and clicks it. If someone within some period of time visits your site using this URI it means he verified his email.

Nothing more, nothing less. It has absolutely nothing to do with any forms or logins 😀

Hope this helps!

elo's avatar
Level 3

@aurawindsurfing Sure that helped. But let me explain, I am already able to verify the user email and was thinking about how the whole thing would work on the front end. For some sites, when the verify email link is clicked the user is redirected to the login page.

Should I be redirecting to the web app login page too?

I tried this code

public function verify(Request $request)
{
    $user_id = $request->id;

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

    $date = date("Y-m-d g:i:s");

    $user->email_verified_at = $date;
    $user->save();

    return redirect("http://www.domain.com/login");
}

and two things should happen when the user clicks the verify link

  1. User email_verified_at is set to $date (successful)
  2. User is redirected to web app login page (failed)

Redirect fails with this message

The requested URL /login was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

But if i enter the same url http://www.domain.com/login in the web browser, I can see the page. What is wrong with the redirect?

EslamAhmed's avatar

Well, it's up to you if you want to verify the email and log the user into the system or just verify the email and let him login by himself. But I prefer to let the user login by himself after the verification.

To redirect the user, just use the redirect method https://laravel.com/docs/5.8/redirects#creating-redirects

return redirect()->route('login');

______

If you have the URL not found issue, maybe you have a fresh apache installation? if yes you need to run these commands and try again

sudo a2enmod rewrite
sudo service apache2 restart
aurawindsurfing's avatar

Hey @elo

If you redirect outside of your application then you should use:

return redirect()->away('http://www.domain.com/login');

if you redirect within your application then you should do:

return redirect("/login");

Hope this will work for you.

Please or to participate in this conversation.