Laravel 5.8 – From The Ground Up
Lesson 22 of 52 · Laravel 5.8 Tutorial From Scratch – e22 – Artisan Authentication Restricting Access with Middleware · Education
Laravel 5.8 Tutorial From Scratch – e22 – Artisan Authentication Restricting Access with Middleware
in the previous episode we added a way for our users to register and create accounts in our app and we did all that in a single command however now it's
time for us to start locking up certain parts of our application so that only logged in users have access to this is a very common thing in websites as you of course will have restricted areas and
you'll have public areas now to achieve being able to lock certain parts of our application we need to introduce a brand new concept that we haven't touched up on before and that is middleware so what
exactly is a middleware now as the word suggests a middleware stands in the middle you might say well in the middle of what in very simple terms a middleware stands in between the request
and the response and the way it does that is by executing certain code now the code will either give it a green light or a red light so if the middleware gives it a green light it
will execute certain code and move on to the next middleware if it gives it a red light everything stops and then we basically execute some failsafe code in the case of authorization typically if a
user requests a page for which you need to be logged in for what it does is it redirects you to the login page basically assuming that you forgot to log in and you're used to this sequence
right so you request a page and all of a sudden you get the login page you say oh my session must be expired let me log back in and then you redirect there back to the original page that you requested
so that is a middleware in action that's what it does your request arrives at the middleware that is handling authorization and all of a sudden it realizes you're not logged in so it says
wait a minute let's show him the login form so that way the user can log in and either proceed or not now if it arrives there and you are logged in it simply just keeps going it does not redirect
you to the login page and that's a exactly what the auth middleware does in laravel so let me show you where they are set back to phpstorm go into app HTTP and middleware
so this middleware directory is what all of the middlewares are stored you can add as many middleware as you like but the ones inside this directory right now is what laravel ships with so what do
some of these do for example there's one here call check for maintenance mode and we haven't really talked about it just yet but since we've arrived here I'm gonna show you how to put your
application in maintenance mode let me head to Chrome I'm gonna hit refresh and of course our page is loading now if we go to our terminal and we ran the command PHP artisan down it puts our
application in maintenance mode and so let's see what the user would get refresh so it says 503 service unavailable this is what you would do to your application
if you're running some upgrades or changing some code in production and you just don't want your users seeing you while you're maintaining your page when you're ready to bring it back up you
will run PHP artisan up and now the application is live and if we refresh we get our application back so that is handled through a middleware what happens is that the request comes in it
checks it fits down for maintenance and if it is then it stops the request and it actually just sends back a response of 503 so that's one of them let's look at another one
then here's one redirect if authenticated this is typical right if you are authenticated then there's no reason for you to be redirected to the login page
instead you redirect it straight to home then that makes total sense there's a couple of other middleware here but the one that we are interested in is this authenticate so this is the one that we
need to tag on to our customers list to be able to control who has access to it there are two ways that we could apply a middleware at this point so the first one I'm going to show you is at the
route level so what you can do is you can lock up any route so I know that I want to lock up my customers out so right after resource I can tag on one method and say middleware and we can
say off use the off middleware okay let's hit save let's go back to the browser and now let's visit my customers list so you can see my customers list but that's because I am logged in I'm
gonna log out and then I'm gonna visit my customers list again so now this time I am redirected to the login page I can still see contact and about and home but as soon as I try to see my
customers list I no longer can see my customers list let me go ahead and login and now I can see my customers list I can edit a customer I can add a new customer I can do everything that I
could before but that's because I'm logged in the minute I log out again it will not let you do that as a matter of fact even if I was to enter the address like create address you're still
redirected to login what if I try to edit one nope you're still going back to the login page so just like that we've locked up the customers list and only registered and
logged in users are allowed to see that page so that's the first method I'm gonna delete middleware and let's just make sure that everything is back to working customers list yep so now our
customers are no longer locked up they're open to see I'm not logged in but I could still see customers so the second way that you can add a middleware is do it at the controller level so
let's do that one now let's go back to phpstorm I'm going to close my routes file and let's check out the customer controller and so I can add a construct method up here and I can call this
middleware and I can pass in the exact same thing off hit save come back hit refresh and now we no longer have access to our customers list now here's another nice little feature
if we added another method call right afterwards you see that we have two methods available only and accept so we can actually do this by method names right so if we only want
to lock up say the create method but we still want to allow edits then you can do that so what I'll actually do is I'm gonna say I want to lock everything up
except my index so I still want guests to be able to see my customers list but if you try to create one or you try to delete one or edit one for that you do need to be logged in all right let's
check it out I'm gonna refresh notice how I am not logged in so my customers list is visible now if I try to add a new customer nope you can't do that okay back to customer list what about
view details nope also can't do that and certainly we cannot edit one and we cannot create one so now we've been able to lock up everything but the index method and if you need the inverse of
this you would use the only so that's a nice way for you to be able to do things like comments for example typically comments are visible publicly you wouldn't really want to lock up your
comments but you typically wouldn't want a guest to be able to add a comment they're gonna add a comment to a section of your website then they need to be logged in and that makes sense so this
is sort of the same approach we're gonna let guests view all of our customers but of course you're not authorized to edit or create one unless you are signed in in my particular case I do want to lock
up that customers list altogether so I will remove that and now everything is locked up back to customers list and says nope you got to be signed in to be able to do that so just like that we can
protect an entire controller or an entire route or we can choose which parts of a controller we want to lock up unless you're logged in so those are the basics of middleware and authorization
in laravel five-point-eight
Let’s touch up on the basics of what a Middleware is and explore ways that we can apply one to require a user to be logged in and authenticated to view our customer list.
For the best experience, follow along in our interactive school at https://www.coderstape.com
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!For the best experience, follow along in our interactive school at https://www.coderstape.com
It’s time for us to talk about registering users, login in users and resetting passwords. Up until now, a very daunting task, but with Laravel, it’s as simple as running php artisan make auth and everything just works. Let’s working on adding authentication to our Laravel application.
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!