Manual Auth Overview0:00
Okay, let's take a look at authenticating manually without using any of these packages. We can make use of Laravel's built-in auth classes and Auth facade to do this. Again, you can always just reference how it's being done in Breeze or Fortify, which we'll do in this video here. So as always, I have a fresh Laravel app here, and I've already set up the layouts like we did in the Fortify chapter. So I have the app layout for logged-in Vue and the guest layout for non-logged-in Vue. So let's start with registration. I'm going to go into the Breeze project, which I have here, and I'm going to take the routes for registration because I want them to be named the same.
Registration Routes and Controller0:32
I'm going to go into the Breeze project, which I have here, and I'm going to take the routes for registration because I want them to be named the same. So one to view the form and one to actually register, but we'll change the controller name in our project. Let's grab that, put it into our routes file. Okay, so we have this RegisteredUsersController. We'll name ours RegisterController, and we're making use of the create and store methods. So let's go ahead and create that now. php artisan make:controller, and I'm going to put it in an auth folder. Let's call it RegisterController.
php artisan make:controller, and I'm going to put it in an auth folder. Let's call it RegisterController. Let's make it resourceful. Okay, and let's make sure that added it to the auth folder, and it did, and we only need the create and the store method. So this will return a view for our registration form. View will put it in an auth folder and call it register. Okay, go ahead and create that. Open our views. Let's make a new one called auth and register.blade.php.
Open our views. Let's make a new one called auth and register.blade.php. Okay, so I'm just going to paste in the form here, so you don't have to watch me create a form. We are making use of that guest layout that I created. We have errors here. If there are any errors, and here's the form we're posting to /register, and it's a POST request, and we have fields for the name, email, password, and password confirmation, and the submit button, and I'm not going to do any styles here, so I apologize for the ugly form.
and the submit button, and I'm not going to do any styles here, so I apologize for the ugly form. Okay, save that, and let's make sure to first spell this correctly, RegisterController, and make sure to import that. Okay, so if I did that correctly, if I refresh this page, nothing is showing. Did I do something incorrectly? Let's try going to the register route. So that works. So on the welcome.blade.php, there should be a check here to check if the register route exists.
So on the welcome.blade.php, there should be a check here to check if the register route exists. So if I look for register here, so it does have the register. Not sure why it's not showing. Okay, so it's not showing because it's checking for a login route as well. So it'll show up after we do login. So let's just go back to registration for now, and let's work on the store endpoint. So back to RegisterController. So what do we want to do here? Let's do some validation first, and then we want to create the User.
Creating Users on Register3:18
So what do we want to do here? Let's do some validation first, and then we want to create the User. Actually, let's start with that. So $user is User::create, and we have a name, email, and password. So let's do name. That's coming from the $request, name, email is email, and we have a password as well. And let's use the Hash facade. So Hash::make($request->password). Okay. Make sure to import User and Hash, okay, and Hash.
Okay. Make sure to import User and Hash, okay, and Hash. And it's also redirect. For now, we'll just redirect back to well, there is no login page yet. So let's just redirect back for now. Okay. Oops. return, redirect, or actually, we can just do back like this. And let's see if this works. We have no validation yet, but let's just see if it populates our database.
And let's see if this works. We have no validation yet, but let's just see if it populates our database. Okay. So let's make a new User, Andre. And if I did this correctly, it should redirect back here, and it does. Let's check our database. I believe I have it open here. Check our users table, and there it is. So let's quickly do some validation, and then we'll redirect the User back to the logged in dashboard.
Adding Registration Validation4:49
So let's quickly do some validation, and then we'll redirect the User back to the logged in dashboard. We'll work on that as well. So for validation, I should do request->validate. And we just have the name, email, and password. So let me just grab these actually, oops. Okay. And for name, let's just make it required. For email, let's make it required as well. And also has to be an email.
For email, let's make it required as well. And also has to be an email. And also it has to be unique on the users table. And for password has to be required as well. Oops, forgot to close that. So it has to be required, and we have to confirm it, confirmed. Let's say minimum eight. Okay. So let's try that out. I believe my form has the output for errors.
Dashboard Route and Redirect5:45
So let's try that out. I believe my form has the output for errors. So if I just press the register button, I do have the errors there. So let's go ahead and add a view for the logged in user. So a dashboard. So back to our code. Let's go to our routes file, and I'm going to paste in a route for a dashboard. So let's just go down here and paste that in. Sorry, I don't have the setting to scroll up at the end of the file. I have a new computer here, so it's not set up completely yet.
Okay. Let's paste that in. So now this is the logged in view, and it's extending the X app layout. So the logged in view. So again, it's the app.blade.php. And we have this menu here. And since we have the route for dashboard, we can add that in here. Okay. Route::dashboard(). Okay.
Route.dashboard. Okay. And did I add that? Yeah, I did. Now we can manually log in the User in our RegisterController after they register. And to do that, we can make use of the Auth facade. So Auth::login, and we just give it the User we want to log in. And import Auth. And now instead of redirecting back, let's redirect to that new dashboard. So return redirect.
And now instead of redirecting back, let's redirect to that new dashboard. So return redirect. And there is a spot in RouteServiceProvider named home, which we can make use of. So that's where you would define where the logged in dashboard is. So RouteServiceProvider. And by default, it's home. Let's change it to dashboard. Okay. Did I do everything correctly? Make sure to import RouteServiceProvider.
Did I do everything correctly? Make sure to import RouteServiceProvider. And I think that should be good. Let's try again. Make another User. And if I did everything correctly, this should log us in to the new dashboard. And it does. And we are now logged in. Okay. So now let's work on logging out.
Implementing Logout8:23
Okay. So now let's work on logging out. And I believe there's a section for logging out in the docs here. And here it is. And there's even some code here. And it says to make sure to invalidate the session and regenerate the CSRF token right here for security purposes. So let's do something similar to this. First, let's make a route for logout. So back to our code, back to our routes file.
First, let's make a route for logout. So back to our code, back to our routes file. So let me duplicate this one and we'll make a logout route. So it's going to be named logout. And we'll put it in a new controller called LoginController, which we'll make. And we'll make it the destroy method. Malware is off. So you have to be logged in to logout. Make sense? And the name is logout.
Make sense? And the name is logout. So let's go ahead and make that controller. Okay. Let's call it LoginController, like I said. And resourceful. Okay. I have an error in my routes file. Sorry. No semicolon here.
Sorry. No semicolon here. Try again. Okay. And let's go ahead and open that up. LoginController, destroy. And let's see what the code is in here. We have to accept a request because we're invalidating the session here. So let's do that. And let's just grab all of this here, paste that in.
So let's do that. And let's just grab all of this here, paste that in. And again, we have to invalidate the session and regenerate the CSRF token for security purposes. So save that. Let's go into our app.blade.php. This is where the logout link is. So this link right here, right here. And since it's a POST request, did I make it a POST request? I did.
And since it's a POST request, did I make it a POST request? I did. We have to make use of the form here. So let's do, where is it? The action. So it's going to be logout, or I'll make use of the name here for logout. And there's already some JavaScript in place to submit the form. So let's see if this logs us out correctly. So let me just refresh, logout. LoginController does not exist.
So let me just refresh, logout. LoginController does not exist. I didn't import it. So I'm still logged in. So import that in our routes file. Here. Okay. Save. Try again. Sorry.
Building Login Flow11:20
Now let's work on the login form. So again, let's just take a look at the routes in Breeze, and we'll steal these ones and make our own login form. So let's do that. Okay. So I'm logged in. I'm logged in. I'm logged in. I'm logged in. I'm logged in.
I'm logged in. I'm logged in. Okay. So now we can make our own controller. Back to our routes file. Let's put it underneath register here. And I'm going to make use of the LoginController that we made. And now these links should show up in the welcome.blade.php. There we go. So login.
There we go. So login. We have to make a new view for that. So let me just add that login.blade.php. And again, I'm just going to paste in the form here. So again, errors, the form posting to the login endpoint, email and password and the remember me checkbox and the login button. Okay. LoginController. Let's go ahead and we don't need these.
LoginController. Let's go ahead and we don't need these. So we only need create and store. So same thing. Return view of off.login. See if that works. Okay. And let's go ahead and work on the endpoint here or the store method to actually log in. So let's grab the credentials from the request. So we only need email and password.
So let's grab the credentials from the request. So we only need email and password. Okay. And the method we want to make use of here is also on the Auth facade. So Auth and it's called attempt. So let me import this before I forget. Okay. It's already imported. And this takes in the credentials as an array, which we already did using the request only method.
And this takes in the credentials as an array, which we already did using the request()->only() method. And the second parameter, the optional parameter, is the rememberMe. It's a Boolean. So we can do request()->filled('remember'). So it's true if it's checked and Auth::attempt() returns a Boolean. So we want to wrap this in a conditional. So if Auth::attempt(). So if it's successful, we need to regenerate the session and you can find this in the docs here for authentication before we regenerate here and here and says you should regenerate.
So if it's successful, we need to regenerate the session and you can find this in the docs here for authentication before we generate here and here and says you should regenerate the User session to prevent session fixation. And this is pretty much what we're doing. So let's grab this request session, regenerate and then redirect to the dashboard. And I believe there's also some error checking here, which we'll grab as well outside of the conditional. And I'm also going to return the input if there's an error. So with input and just the email request only email.
So with input and just the email request only email. Okay. So now if I did this correctly, we should be able to log in using the login form. So let's just go ahead and try andrea.com password. Let's check this box and it's login. And it did work. Cool. So now we can register a User, log the User in and log the User out all using Laravel's built in Auth facade. In the next video, we'll take a look at forgot password and resetting passwords, which also
built in Auth facade. In the next video, we'll take a look at forgotPassword and resetting passwords, which also makes use of Laravel's internal classes.
