در حال بارگذاری ...

Generating Auth Scaffolding0:00

When it comes to authentication in Laravel, yeah, it's kind of changed over the years. For example, at one point, out of the box, you got these sets of views that made it really easy to register, log in, reset your password, all of that stuff. But then, some people complained, so it got removed. But now, I'm happy to say that it has been returned in the form of an artisan command. So if we scroll up, you'll see this new command in Laravel 5.2, php artisan make:auth. Now, let's just try it out. I have a fresh install of Laravel, php artisan make:auth, and you'll see it creates a handful of views, it installs a HomeController, and it also updates your routes file to register all of these endpoints. So you'll see we have this views/auth directory,

and it also updates your routes file to register all of these endpoints. So you'll see we have this view auth directory, and this will have your bootstrap-specific login page, and register page, and password reset, and password confirmation, and a dashboard, and all of the basic stuff you would expect. Now, a quick note on this, it's really intended to be used at the beginning of the project, potentially right after you install the framework. So, yeah, you could still run this six months down the line, but just be aware that it will create a HomeController, and it will update your routes file to register these endpoints.

Running and Viewing Pages1:05

but just be aware that it will create a HomeController, and it will update your routes file to register these endpoints. So just something to be aware of. All right, so now, check out how cool this is. I'm going to do php artisan serve, and now, here's what we get. So think about it, this is all I have done. I ran laravel new project from the command line, and then I ran php artisan make:auth. That's it. And immediately, we get just kind of a nice, typical layout.

That's it. And immediately, we get just kind of a nice, typical layout that you can then customize however you want. Now, check it out. We have our login page. We have your registration page. We have a password/reset page, as well as the confirmation. All of this stuff manually takes an incredible amount of time to write, especially if you like building up little weekend projects and such. So it's nice that now, in Laravel 5.2, you just have instant access to it.

Customizing AuthController Behavior1:46

especially if you like building up little weekend projects and such. So it's nice that now, in Laravel 5.2, you just have instant access to it. Let's try it out. I'm going to register John Doe, john@example.com, and password. Okay. Instantly, we're signed in, and we have a new User. Now, the way this works, if I go back to them, is you'll see Laravel includes an AuthController out of the box. So this is where everything's handled. For example, after you do sign in,

So this is where everything's handled. For example, after you do sign in, it's going to redirect you to /home, and you can see that represented here. But if you want it somewhere else, for example, dashboard, then just update that. No problem there. And then also, if we scroll down, you'll see, for example, your validator. If you need to modify this, then you would update it there. And you'll see we can go to auth/register. And, yeah, you'll see this is sort of a Twitter Bootstrap-specific layout.

And you'll see we can go to auth/register. And, yeah, you'll see this is sort of a Twitter Bootstrap-specific layout for a typical registration page. And you'll see it includes all of the errors and stuff, all that annoying stuff that you always have to refer back to the documentation to figure out what the correct class name is, for example. You just get that out of the box, and then you can customize it however you want. But anyways, if we go back to Chrome, we are authenticated. So this page, remember when I said earlier that Laravel creates a HomeController? This is sort of like your dashboard.

Reviewing Auth Routes3:00

So this page, remember when I said earlier that Laravel creates a HomeController? This is sort of like your dashboard. So you'll see that we apply the auth middleware to all of the routes. That will ensure that you are authenticated in order to access any of these pages. And then we have this one, index, that loads the home view. Now, though, if I go over to my routes.php file and we scroll down, you'll see that we received this bit of code when we ran make:auth, which means we can get rid of that. Now, we do register an endpoint for the HomeController, but also you'll see route::auth.

Now, we do register an endpoint for the HomeController, but also you'll see route and auth. So what's happening is Laravel is just kind of tucking away all of the miscellaneous routes that have to be registered. Now, it'll tuck that behind the Illuminate\Routing\Route class. And let's see, auth. Yeah, you'll see, yeah, lots of routes to register here, so it just tucks it behind this method here. So endpoints for login, when you post the login form, when you want to log out, when you want to register,

So endpoints for login, when you post the login form, when you want to log out, when you want to register, and when you want to reset your password. Now, you'll likely need to refer to these, so just remember you can always run php artisan route:list, and this will show you every, if I zoom out, it'll show you every endpoint that you have for your project, and that's a good reference to have. So if we go back, well, we can log out. We have a nice dropdown here, and we're logged out.

Testing Password Resets4:16

So if we go back, well, we can log out. We have a nice dropdown here, and we're logged out. If I go back to home, yep, it protects us. Let's try out password reset, and we'll say john@example.com. But real quick, before I send this, I want to update my mail driver. I can do that through my .env file, and if I scroll down, yeah, for local development, I do want to use the log driver, and that way it just logs it straight to the storage logs laravel.log file. Okay, so let's clear that out, go back to Chrome, and see if it works.

and that way it just logs it straight to the storage logs laravel.log file. Okay, so let's clear that out, go back to Chrome, and see if it works. Send password reset link, there we go. So if we go back, yep, there we go, here's our email. So let's try it out in the browser, and now we can register a new password, new password. Once we reset it, it will update that record in the database table and instantly sign you in. Now, one final thing to take note of, Laravel includes authentication throttling right out of the box as well.

Authentication Throttling and Views5:11

Now, one final thing to take note of, Laravel includes authentication throttling right out of the box as well. In the past, this was stuff we had to write manually. It was always kind of annoying, and further, it was really easy to kind of skip over that just because you want to get moving quickly. But yeah, now you don't even have to think about it. So let me show you how that works. We're going to do frank@example.com, and I'm just going to insert an incorrect password over and over.

This way, after five incorrect attempts, something funny might be going on, so we're going to limit them. Now you'll see, even if I type the correct password, it won't work. Now, just one final thing before we finish up. If we go into resources, views, yes, you have this auth directory, and this was built up when you ran php artisan make:auth. So you can inspect these, modify them, do anything you want. But also, Laravel will give you a layout file, and this is really useful in and of itself. So yeah, basically every project will have a layout file,

But again, if you don't want that stuff, you'll see we have this commented section right here that will pull in your Elixir file. So it's very possible you're using something like Bower. If so, no problem. Just delete this, uncomment this, and now whenever you run Gulp, this will pull in your app.css file. And of course, you already know about Elixir, so all that should be old news at this point. Okay, and the same thing will be true at the bottom as well. We're pulling in jQuery, we're pulling in the Twitter Bootstrap JavaScript file.

Okay, and the same thing will be true at the bottom as well. We're pulling in jQuery, we're pulling in the Twitter Bootstrap JavaScript file. It's assumed that you will modify this, so you can move that into your build process and then just uncomment this line of code that loads your app.js file. So yeah, that should do it. Just a quick review of the new authentication views in Laravel 5.2.

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