Ace's avatar
Level 7

where in fortify is the default verifyemail redirect defined

I have the following link from the verifyemailnotification generated in fortify:

http://sanctum.test/email/verify/48/d3e7871cdf46101eec126daa2b8265830e8ee30c?expires=1690202302&signature=4089e5544eb078832039d3164b55165887c61f61519fa5ce2c68d8823fcac2f5

It redirects in my case to api/login.

where can I define a different url?

my goal is to create a url to open a mobile app specific page

0 likes
10 replies
LaryAI's avatar
Level 58

In Laravel Fortify, the default redirect after email verification is defined in the VerificationController class. To modify the redirect URL, you can override the redirectTo method in your own VerificationController class.

Here's an example of how you can define a different URL for the redirect:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\RedirectResponse;

class VerificationController extends Controller
{
    /**
     * Redirect the user after email verification.
     *
     * @param  \Illuminate\Foundation\Auth\EmailVerificationRequest  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function redirectTo(EmailVerificationRequest $request): RedirectResponse
    {
        // Define your custom redirect URL here
        return redirect()->to('https://example.com/mobile-app-page');
    }
}

Make sure to update the namespace and the redirect URL according to your application's needs. After creating this VerificationController class, you need to update the fortify.php configuration file to use your custom controller.

In config/fortify.php, locate the features array and update the email_verification section to use your custom controller:

'features' => [
    // ...
    'email_verification' => [
        'enabled' => true,
        'controller' => App\Http\Controllers\Auth\VerificationController::class,
    ],
    // ...
],

By doing this, the email verification redirect will be handled by your custom controller, and you can define any URL you want in the redirectTo method.

Ace's avatar
Level 7

@LaryAI it is not redirecting, same as before. cleared config and cache

Snapey's avatar

Lary is giving you the action to take when the email is verified.

What you are asking for is to change the URL that is sent out in the email?

Ace's avatar
Level 7

I found the issue : I am building a view-less fortify for a mobile app and in the Authenticate.php Middleware , it redirects if not authenticated to the login route, and the login route was not a named route.

protected function redirectTo(Request $request): ?string
    {
        return $request->expectsJson() ? null : route('login');
    }
}

also doesn't Fortify have an implementation for updating the user model after the link was clicked?

Snapey's avatar

I researched an answer to your original question, so I might as well still paste it incase it helps others in the future;

In the User model, add the following function

    public function sendEmailVerificationNotification()
    {
        $notification = new VerifyEmail;

        $notification->createUrlUsing(function () {
            return 'https://my_custom_url.com';
        });

        $this->notify($notification);
    }

createUrlUsing() should return the URL that the user is sent to when they click the link.

1 like
Snapey's avatar

also doesn't Fortify have an implementation for updating the user model after the link was clicked?

The only thing you have to do is set the current timestamp on the users.email_verified_at column

here's the code that does it;

public function markEmailAsVerified()
{
    return $this->forceFill([
        'email_verified_at' => $this->freshTimestamp(),
    ])->save();
}
1 like
Ace's avatar
Level 7

thanks a lot, but where can I see this in the laravel docs?

Snapey's avatar

@Ace nowhere to my knowledge. Why do you need to see it in the docs?

Ace's avatar
Level 7

@Snapey actually the reason is that the Fortify docs are quite lean and do not show the capabilities possible. Especially for setting it up as an Api for a mobile app I find myself overriding the built in flow and features quite a lot.

ahmedde's avatar

I second that! The docs are missing too much, especially if you are using Laravel as API only. You have to google lots of obvious stuff that is not in the docs.

Please or to participate in this conversation.