تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Email Verification Overview0:00

Okay, let's take a look at email verification. So after registration, you might want to check if your User's email exists. Let's see how we can do that. So first, let's take a look at an example of how it works in Jetstream. So let's actually let's go to the code. Make sure the feature is on. And then in your User model, make sure you implement MustVerifyEmail. And then I have my mailer set to log. And my log is empty. So let's see if this works.

Jetstream Verification Demo0:29

And my log is empty. So let's see if this works. So let me register a new User. Okay, and let's register. And now we get this form. And we have two options. We can either log out or resend the verification email. So we'll create this view. But let's see if we get this email first. So it should be in my log.

But let's see if we get this email first. So it should be in my log. And there it is. And once I hit this endpoint, then my email should be verified. So let's grab this. And I believe it logs you in as well. So let's hit this route. And there we go. I am now logged in. So let's make this work in our Fortify app.

Enable Verification in Fortify1:10

I am now logged in. So let's make this work in our Fortify app. So let me just log out here. And this one is our Fortify app. So back to our code, make sure we are in the correct project. Let me open up my log here. Actually, it's empty, so it won't show up. And let me change my mailer to log. Where is it here? mailer.

Where is it here? Mailer. Log. And let's enable that feature in our config. Down here. Where is it? Right here. Okay. Uncomment. Let's go to our User model.

Uncomment. Let's go to our User model. And let's implement mustVerifyEmail. Implements mustVerifyEmail. Okay. And now, let's go ahead and set up that view according to the documentation. So it's off. VerifyEmail. Let's grab this. Put it in the service provider.

Create Verification View2:06

Let's grab this. Put it in the service provider. FortifyServiceProvider. Let's put it with the others. And let's go ahead and make that view. So it should be in resources/views, and in auth. So let me just duplicate this one. Let's call it verifyEmail.blade.php. Okay. And just say verifyEmail.

Okay. And just say verifyEmail. And just basically copy what's in Jetstream. So let's look for it here. There it is. So we have a message here. I guess we can just copy this. And I guess we can leave the errors in there as well. So that's just a generic message. Let me get rid of the translation here just so it's clear.

So that's just a generic message. Let me get rid of the translation here just so it's clear. And I guess I'll just leave the classes in there since it's not going to do anything anyways. Next is the confirmation message, if it did work. It's looking for a status session variable named verificationLinkSent, if it was successful. So let's put that underneath our errors. Okay. And I guess I don't need to get rid of the translation. Next is the form here.

And I guess I don't need to get rid of the translation. Next is the form here. And if you remember, we had one that resent the verification email and one to log out. So let me just grab these two actually should be the same endpoint. There should be an endpoint named verification.send after I enabled the feature. So let's look for that. php artisan route:list, grep verification. And there it is, verification.send, okay. So let's put this in our project. Let's get rid of this form here and paste that in.

So let's put this in our project. Let's get rid of this form here and paste that in. So this will resend the email and there is no X chat button. So let's just change this to a button. Okay. And this is a logout button. So that should work as well. And let's see if this works. Back to our app here. Let's go ahead and register a new User.

Add Verified Middleware4:26

Back to our app here. Let's go ahead and register a new User. Okay, register. And that did not work. I am logged in now. Actually if you look at the log, it did work and it did send out the email. So you can see the link here to verify your email address. So the reason it logged me in, let me just copy this actually, is because I did not put the verification middleware for our route. So if you go to app/Http/Kernel.php right here, you'll see a middleware for verified.

the verification middleware for our route. So if you go to app/Http/Kernel.php right here, you'll see a middleware for verified. So we have to put that in our route here, right here. So let's say verified, okay. And now if I refresh, we should get that form and we do, but it looks like I didn't name it correctly. Yeah. I think I did it wrong, sorry. verify.

Verify. Okay. And now there it is. And if I hit that route, which I have in my clipboard, this should verify it and log us in. And it does. Cool. Let's just verify that the resend button works as well. So let me just migrate fresh again. Let's make a new User here.

So let me just migrate fresh again. Let's make a new User here. Let's go to register. So let's go ahead and register. Okay. And let me clear my log here. There of our log, clear everything and let's resend the verification email. Okay. And there it is. Cool.

Customize Email Templates6:37

Cool. Okay. One more quick thing I want to show you has nothing to do with Fortify, but say you wanted to change the look of your email. So I have an email client here so we can render our email. So let's go ahead and sign up for a new User again. And this should send us an email and it does, there it is. So if you want to change the look of this, like I said, this has nothing to do with Fortify, but the default templates in Laravel. So that's how it looks.

but the default templates in Laravel. So that's how it looks. And if you want to change that, you just have to publish, say php artisan vendor:publish the notification that it's using and also the mail templates publish. So 19 for Laravel notifications and also 18 for Laravel mail. So now if we go into, where is it, vendor, we have notifications and here's the email and you can customize this a bit, but this is mostly just the copy. But if you look at, say for example, see how it looks, this button over here, I believe there's a button here, right here. This is a component, as you can see, and you can modify that in here.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟