Laravel project #12 Protected Routes | Auth | Middleware

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to the lateral project series my name is Anil and in this video we will see that how we can check those implications with the routing in the laravel so we will check the two conditions one if they choose it is no not logged in then he cannot access the home page list add anything alright and if the user is logged in then user should not go on a login and a registration page because this is already logged in and he should not be logged in again in register again alright so now let's see that how we can do it so these are the steps of our video so first of all we will make our middleware then we will register it then we will apply it and after that we will make some logic for urges for login and logout alright so guys keep in mind that many guys are confused about the material that how they can use the middleware property properly and why they actually have to use the middle bit this is the perfect example for that alright so now let's start so first of all let's clear out come online and just make a middleware so for that I am just going to write here PHP artisan make middleware in the middle of a real name so let's say my middleware name is custom mode like custom let's hit the enter now you can see that a middle middleware is created and you will find this middleware inside the app HTTP middle rail here it is so for a moment we can check that this is working or not I am just going to put here hello from middleware alright and after the creating it we have to register it inside kernel dot PHP so I just want to register it in the group the middleware because sometimes we just want to use the middle bear on some specific path note on our ol path so we can make a group for that all right so let's say moment I just want to mention it inside the web group and here I can just register my middle-earth so for that you just need to change app HTTP middleware and your middle real name so I can just make a little bit name custom auth why I'd make the name of the customer because the earth is already provided by the Lara that's why I just use the custom mode so after doing this now let's go on a web dot PHP and let's apply it for applying we must have to make a route with the group method and in the group method we have to use an array and in the array we can use the middleware and in the middle where I can just mention my middleware name like web and here let's make a function and in the function I can just use the arrow function oh sorry anyways function here and let us copy these old routes and we can make inside it like this so now let's see we are able to see this hello from MD or not' because it should be come on every page yeah this is working for every page you can see that all right so now everything is ready so now first of all I just want to make condition that if a user is logging then he should not come again on the user login or register register wait for example let's say I just want to login with Tony and here you can see that there is an or out for go for on the login or agitation but still I can go on these routes via directly putting the URL like this one is already logged in but still he is able to see in the logins user page so this is bad so how we can prevent it so now let's come inside the middleware and just I am just going to show you the echo dollar request and what and this will provide the current path was what is our current path so now let's check I'll be able to see YES on at least on add-on search now we can see that everywhere the path here all right so let's take it inside a variable like path like this now let's make a condition if let's hide this sidebar so that we have a more space to with view the screen this right here if dollar path is equal equal to log in and you can just make it again check if our path is equal equal to resistor and we can make a end condition hole and here we can just write here and the session get user it says situation is set then it should be a redirect on home page so district on redirect on route so we can just use like this yeah and here I can just use the curly braces like this so now let's check is it working or not so here you can see that okay so she Asian is not found so we have to import the session just use session like this so now you can see that if I'm just going to try on our login then it will meet redirect directly on a home page and let's try with the registration still it will send you in the home page so this is perfect about lowing so now let's say let's close it close it and make open the another one incognito because the session is already there but in the new incognito the regeneration all right so now if user is not logged in then he should not be have authentication to watch the home list ad and search page so if we try to that leave open this page the login page should be directly open alright so now let's see how we can check it so just here just make it as if and in this as if we have to just simply check here if dollar path is not equal to login and Ashley is not present all right and session is not present then she should be go on let me check is there any issue yep then he can read act on a login page alright like login all right so and the same thing we can also check it with the registration page also so if it's register then it should be go on register page or login pages only alright so now let's try to check it if session should not be set here all right so let's make it like this now let's start to access it now you can see that we are on a sew-in page so still I am going to try going to add home any page it will directly take me on the low game so if I just used here Tony and logged in here now you can see that I can access any page but I cannot go on a login page so this is a perfect example about authentication and drought protective protection so you still have any kind of confusion you can ask me in the comment box don't forget to subscribe my turn and like this video bye bye take care
Info
Channel: Code Step By Step
Views: 14,860
Rating: undefined out of 5
Keywords: laravel 7, laravel project, laravel project tutorial, laravel restaurant project, laravel protected routes
Id: ZDZ7BTOog50
Channel Id: undefined
Length: 8min 55sec (535 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.