geerizzle's avatar

How/where to adapt Email Verification?

I've setup an ecommerce solution like this:

Step 1: User enters email & name. On submit, user is created in DB with random password. Step 2: User logged in automatically. Completes checkout with address & makes payment. Step 3: User then sets account password and some other things. DB updated with their password. (Step 4: They verify their email to get full access to user dashboard.)

I wanted to do this to minimise the number of fields they need to complete before paying and also to capture email address for remarketing if they abandon the cart.

I want email verification. The problem is that right after step 1 the verification email is sent. So potentially they receive it while still at step 2, they click on it to verify but they don't have their password created yet.

How can I disable the automatic sending of the verification email and then manually send or trigger it after step 3?

0 likes
6 replies
mironmg's avatar

You need to edit the Registered event that is being triggered by the register()method within RegistersUsers trait file. Basically you can extract the register method in the RegistrationController and comment out that event.

1 like
geerizzle's avatar

@MIRONMG - Thanks I had a look and so I can stop the action - do you know how I would manually trigger it?

mironmg's avatar

@GEERIZZLE - You can track down the listener for Registered event and see what it does, then replicate that in a controller by yourself and do it whenever you want

Snapey's avatar

When a user is registered an event is fired. Its caught in the EventServiceProvider

use Illuminate\Auth\Listeners\SendEmailVerificationNotification;

//
//

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

I'm a bit puzzled though why you are going down a registration path when all that is required is to create a User model User::create([])

geerizzle's avatar

@SNAPEY - I guess I'm not entirely sure!

My new user form posts to /register and then in the RegisterController I have the below.

If I recreated in another controller rather than do it through registration then I assume the email verification email isn't sent. Ill have a look.

I just realised I can send the verify email anytime using: $user->sendEmailVerificationNotification(); so it should be straightforward enough.

/**
 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            // 'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }
    
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make(random_bytes(12)), 
        ]);
    }
Snapey's avatar

registration also forces the user to be logged in and diverts control from your preferred user journey.

Just User::create with the required attributes

Please or to participate in this conversation.