In this episode, we dive into implementing email verification for users, whether they sign up using standard email/password or through a social provider like Twitter or GitHub.
We start by adding some simple login and register links to our page so we can test the different registration flows. Then, we enable email verification by adding the necessary contract to our User model, which makes Laravel require an email verification for users before they can access protected routes (like a dashboard).
Next, we set up MailPip as a local fake mail server. This lets Laravel send verification emails to a mailbox we can check during development, making testing much easier.
After verifying that email verification works for standard registration, we notice that it's not working for users signing in through social providers. We dig into why this is the case and discover that the event responsible for sending the verification email (the Registered event) isn’t being triggered in our social login callback.
We update our social login controller to fire the event when a new user is created. However, we realize firing this every time (even on subsequent logins) isn’t ideal – Laravel is smart enough not to send a duplicate email, but other listeners on that event might still cause issues.
So, we wrap the event firing in a condition that only triggers if the user was just created (using the wasRecentlyCreated
property). We test everything again and confirm that verification emails are only sent when needed.
By the end of this episode, you'll have a robust setup where new users always get verification emails, no matter how they register, and you avoid unnecessary event triggering or email spamming.