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

Hooking Into Fortify Login0:00

Authentication Okay, we're using Fortify for our authentication process, and we need to hook into the authentication flow in order to make sure that we can transfer our session card to our user base card. So whenever a User logs in, the session ID is regenerated. So we want to make sure that we transfer the items before a new session ID is generated, otherwise, we don't know what the original card was. So if we head over to the Laravel documentation.

otherwise, we don't know what the original card was. So if we head over to the Laravel documentation and look up the Fortify package documentation, you can see there's a section on authentication, and you have a section right here that states Customizing User Authentication. If we take a look, you can see that in our AppServiceProvider, we can override how Fortify is authenticating the user by using the authenticateUsing method. So let's do this. Let's copy and paste this section

by using the authenticateUsing method. So let's do this. Let's copy and paste this section and move over to our editor and open up our AppServiceProvider. And finally, we'll add this code into our boot method. Let's make sure that we import all the correct class names. Okay, so this is pretty straightforward. From the request, it grabs the email address, it looks up the User based on this email. Next, it checks whether this User exists. If it does, it checks whether the password

Creating Cart Migration Action1:30

Next, it checks whether this User exists. If it does, it checks whether the password matches the input given by the user via the request object. So straight after a User has been provided to Fortify, it will use this User to log into. So we want to make sure that just before this happens, the card items are transferred over. So let's create an action that allows us to do this. Let's open up our action directory, let's go into WebShop, and let's create a new class,

Let's open up our action directory, let's go into WebShop, and let's create a new class, and let's call it MigrateSessionCard. Let's create a new method, and let's call it Migrate. Now, we'll accept the sessionCard as our first parameter, and our second parameter should be our userBaseCard. So now we want to migrate items from our first card to our sessionCard. So what we can do is we can loop through each of our items, and we'll add it to our card.

So what we can do is we can loop through each of our items, and we'll add it to our card. We still need to make a few changes to our class. So what we're going to do, we're going to loop through each of our items, and we're going to use our AddProductVariantToCard action to transfer these session card items to our new user card. We have to make a few changes to our AddProductVariantToCard class, but we'll get to that in a few seconds. So let's initiate our action and call the add method. First, we're going to pass along our variantId.

Updating Add-to-Cart Action2:49

So let's initiate our action and call the Add method. First, we're going to pass along our variantId. Now, we want to expand our action a little bit so that it accepts the quantity, and then it accepts an additional card so that we can override the action, whether the action should use the cards derived from our factory, or whether we can override it by passing a custom card. First, let's do quantity. And finally, we'll do card.

First, let's do quantity. And finally, we'll do card. There we go. Now, let's head over to our productVariantCard action, and we'll add quantity here. Let's set the default to 1, and update this as well, so... the quantity is set based on this value. And finally, we have our card, which is going to be null by default.

And finally, we have our card, which is going to be null by default. And now we need to ensure that our card variable is used instead of our factory whenever card is defined. So if card is defined, we'll use it. Otherwise, we'll fall back to our factory. Okay, that should do the trick. And finally, when the migration is complete, we want to make sure that we clean up our database and remove our session-based card.

Wiring Migration Into Auth4:06

we want to make sure that we clean up our database and remove our session-based card. So first, we'll delete all the items, and finally, we'll delete the card itself. Okay, let's switch back to our AppServiceProvider, and let's implement our migration action. We're going to say new MigrateSessionCard, migrate, and now we can pass different cards. So if we use our cardFactory, we know we get our session-based card,

So if we use our CardFactory, we know we get our session-based Card, because we're not logged in yet, and this is just before Fortify logs us in. Next, we need to provide our second Card, and this is going to be User Card. Now, this is going to be our existing Card, but in case it doesn't exist, we want to create one. Okay, let's give this a try. Let's switch back to our browser.

Testing Cart Transfer4:54

Okay, let's give this a try. Let's switch back to our browser. Now we're currently logged in, so let's add four different blankets to our card. Now we're going to log out, and now we're going to add another four to our session-based card. So this should give us a total of eight when we log in. And it seems that I forgot to import something. This one.

And it seems that I forgot to import something. This one. Okay, let's try that again. And there you go. Our items from our session-based card were transferred over to our User Card. We had a total of four products when we were not logged in, and once we logged in, it added those additional.

and once we logged in, it added those additional for giving the final result of eight products in our card.

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