YouTubevideo· Jul 8, 2024· 9:49· 3,200 views on YouTube

Make Your Livewire App Secure by Default

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

22 moments
  1. I recently just put out a tweet talking about one of my favorite ways to work with Laravel, especially just to get things up and running quickly and to make things a lot easier while developing. That's Folio, Laravel Folio and Laravel Livewire Volt. Folio is a page-based routing so that you have all of these pages, let's say you have an index.blade.php and then you have an about.blade.php.

  2. Laravel puts that together with Folio so that you have those routes automatically created. And I think Volt, which is the single file components that I love talking and showing on these videos, works fantastic with this because then you have that logic, if you need it, just automatically in those files. Now, I'm going to put out a video talking about how I use all of these things together with Filament, which is a forms

  3. and tables and notifications and even admin panel package that just allows you to use all of this single-handedly look like this. You can have a markdown form or a markdown-based input field in your form, which is a few lines of code. That's why I love it just because it gives you the ability to go from zero to let me get something on the page without much work. Well, as I was digging into this and and preparing to make a video,

  4. I learned about this new package that just came out by Filo, someone in incredibly valuable to the Livewire community, called Wire Elements Livewire Strict. Now, I'll kind of show why this is incredibly popular and why it's I think is incredibly valuable, but it also works with Folio and Volt, which is what got me excited. Now, any property that you have on a Livewire component is

  5. editable from the browser. There's nothing that ties it down unless you claim that property as locked. Now, why is something like this package important? Well, it kind of ties in with another setup that I like to do when I'm building Laravel application. So, why don't we talk about those two things and dive into some code. Okay, now this is a pretty standard Folio page with a Volt component. Folio just basically means I

  6. can have this dashboard folder with an index.blade.php and now accessing {slash} dashboard gives me that page. So, why is this incredibly special and why would we use something like strict Livewire Strict? This package put out by Filo is a third-party package, but I think it works great, especially if you are developing and just want an extra sense of security so that you know

  7. you're doing things the right way. Okay, so this public count is editable on the page by using this increment function, which is look hooked up to this button. So, it just works. But, if I were to open the browser tools here, let's pull this up, pull up in console, I can actually set that count to a specific variable.

  8. Hmm. Well, you can see how this could be a security concern because if I didn't want that to be editable just by anyone, but I wanted it to only be editable by functions or methods within Livewire component, I could use the locked command. And what that would do is if I was to use Livewire attributes locked, then all of a sudden I cannot do that. So, this is just saying that this public

  9. count property is now locked. I could still edit it here using the method that I'm using with this button, the increment method, but if I was to try to edit this variable, I get a error. We'll close this so you can see. It says that you cannot update a locked property. Now, this is great, but what if I forget to update that locked property?

  10. Well, that's where that Livewire package comes in. So, this is Livewire Strict. So, if we were to install this, so that would be composer require and then it would be wire-elements {slash} Livewire Strict and I've already have it installed. So, here's what you can do. Now, in your app service provider, you can just set this to Livewire Strict in the register to lock properties. Now,

  11. what this will do is in our dashboard, I can remove this locked and I automatically have all properties locked by default. So, still works, but if I was to try to change this like this, it's still locked. And again, this works with Volt as well as regular Livewire components. I think this is incredible because it kind of gives you that nice safeguard, which

  12. leads me to one of my favorite features of Laravel, which is the model should be strict attribute within the boot app service provider. So, I think these kind of go hand in hand and what reminded me to make a video about both of them because one of the things that I always hate about developing in in local is if I was to let's go ahead and create a new model. If I was to say make model,

  13. we'll say a post and we'll create a migration for it. Now, I let's just go ahead and whip this up really quick. Create post table. We'll just say that there's a title for this and that's it. That's the only thing for the post, but now we'll go ahead and migrate it. PHP artisan migrate. So, now we have new post. Well, if I wanted to actually create this post, we can say public

  14. title equals and we'll just set it to a blank string for now, but we can say public function create post and what we can do is just say post create and we'll say title is this title. Okay. That's great. Now, let's just go ahead and add a field here.

  15. There we go. We have the title linked to this input and then we have create post button linked to this button and you should just put this in a form. Just for some quick purposes, we're just going to create a new post and we can use the use app models post here. So, create a new post with the title of this title. New post. Okay, looks like it's a locked property, so

  16. that's one of the things we could do. So, what we can do is actually we'll unlock this because we do need this to be edited on the page. Now, what we can do is use this unlocked attribute. And what that's going to be if we grab this from this page, we can say it's going to be unlocked and one thing that you could do is also use the unlock for the specific class itself. So, here we could say new

  17. um unlocked if we wanted to. But for now, we'll just unlock the title and again, this just means that now the user can edit this property. And now because we have model should be strict turned on, usually what would happen is you just don't get an error at all. But now if I want to try this, I get this error that says add title to fillable property to allow mass assignment. This is great because now I can know in my post I actually need to add this fillable property. Again, you can also

  18. have this be protected guarded. To just say anything is able to be edited or fillable other than that property. So, we'll just go ahead and now we can create a new post. Those posts have been created. We just don't get any any information back from the server because we didn't write that. Okay, but there's other great things that use model should be strict applies. This is in the blog post by Laravel

  19. News. Use model should be strict is a great thing to turn on for any new Laravel applications. So, you get prevent lazy loading, silently discarding attributes. That's the one we just looked at. One of my favorite ones that I remember, as well as prevent accessing missing attributes. So, there's two things. First, prevent lazy loading is if you are accidentally lazy loading any eloquent queries without using the proper with documentation

  20. in the sense that you need to also include that second model, it gives an error. Another great thing is the discarding attributes, which we just looked at. And then lastly, if you are trying to call an attribute that is nonexistent. So, for example, the user email, which is misspelled, would then throw an error because there is no email for that user. It's a great way to just catch errors or mistakes before they happen. So, be sure

  21. to check out the wire-elements Livewire Strict if it's something that you like to use on a daily basis like I do and it just might help you stop having as many errors. I know I have them all the time and a lot of times it's hard to pinpoint where are those coming from. Additionally, one thing that's really cool on Livewire Strict is you can actually just have this be to local if that's what you want. I think this Livewire Strict is a

  22. great way to have both in production as well as locally. So, that way you can catch errors before they happen, but also maybe not maybe you can purposefully allow specific properties that you know a user needs to edit like that title property, for example. So, hopefully this shows you a little bit more about ways to just make things easier as you're developing with Laravel and Livewire. So, keep creating.

just you here