‫All right guys.
‫So now we're going to get into authentication.
‫Right now we have create read update delete functionality.
‫But no authentication no user system in place.
‫We do have the users table created already.
‫We have some some dummy users.
‫But we need to to implement that in our app.
‫We want this login to work obviously the register.
‫So the first thing I want to do is just create the views and the controller so that we can at least
‫load the views.
‫We can we can get our database instance in the controller just like we did in the listing controller.
‫So let's let's do that.
‫Let's start off by creating the routes that we need.
‫So I'm going to go.
‫Let's see.
‫Where do I want to put this stuff?
‫Let's.
‫Yeah, I guess we'll just go to the bottom here because now we're dealing with the user controller.
‫So we'll do that down here.
‫So router and we want a get request to slash.
‫Now I'm going to use the auth as the prefix for anything to do with authentication.
‫If you want to do just slash register you can but I prefer to do auth register.
‫And then that's going to go to the user controller, which doesn't exist just yet, and it's going to
‫go to a method called create.
‫Because again, I'm trying to stick to that convention, that rest convention.
‫We're creating a user here.
‫It's a register they're registering, but essentially we're creating a user.
‫We're doing a database insert.
‫So we'll do that.
‫And then we'll also have the log in which will be auth login.
‫And that's more of a it's kind of tricky on how you want to set this up as far as convention because
‫it's not your standard crud.
‫So what some people will do is have a separate session controller and they'll have like session controller
‫create.
‫But I like to keep all this in the same controller.
‫So we're just going to have a method called login okay.
‫You could call it authenticate authenticate or something like that.
‫But I'm just going to stick to login.
‫All right.
‫So we'll save that.
‫Those are the two routes.
‫Now let's create the controller.
‫So we're going to go into app controllers.
‫And I don't know why I have this home PHP there I think I might have did an undo and that came back.
‫So you don't need that.
‫Let's create user controller dot php.
‫And then let's see we're going to we're going to namespace this to app controllers.
‫So let's say namespace app controllers.
‫And then we're also going to be using a couple of things here from our framework.
‫So I mean, I really like how this is set up.
‫We we create our controllers and then anything we need to use that's part of the foundation of our application,
‫the core, the framework we bring that in.
‫So for instance we want to use the database here.
‫And then we also want to use validation.
‫So we're going to bring that in as well.
‫And then we'll create our class A user.
‫Controller.
‫And we're going to have a protected property for the database.
‫So DB.
‫And then let's create our constructor.
‫So public function double underscore construct.
‫And in the constructor is where we want to initialize our database.
‫So we need our config.
‫So config and then require.
‫Base path.
‫And that's going to be config slash db dot php.
‫And then after that we'll create our database instance on that DB property.
‫So let's say new database and pass in that config.
‫Okay.
‫So we have that now we're going to have we're going to create two methods here that at the moment are
‫just going to load a view.
‫So this one let's actually put a comment here.
‫We'll say this will show the login page.
‫And yeah, to show the login page.
‫So it's not going to return anything so void.
‫And let's say public function.
‫Log in.
‫And we're going to load the view.
‫And the view we want to load is users slash user slash login.
‫All right.
‫And then for the create.
‫So show the register.
‫Register page and I'm going to call this create.
‫If you want to call it register you can.
‫But like I said I'm just trying to stick to the naming convention, just specific naming convention.
‫So we're going to load the view users slash create.
‫All right.
‫Now let's let's change that because right now these links they're going to login dot HTML and registered
‫dot HTML.
‫We want to change that.
‫So let's go into our views partials nav bar.
‫And let's change the link from login HTML to slash auth slash login.
‫And then for the register.
‫Let's change that to slash auth slash.
‫Register.
‫All right?
‫Right.
‫That's what I put in here.
‫Yeah.
‫So it'll call the create method.
‫But obviously we don't want to do auth create.
‫So the link will be register.
‫So let's reload this.
‫And if I click on that it's it's correct.
‫But we just don't have the login.
‫We don't have the view created yet but we know that it's going to the right place.
‫So now let's create the views.
‫So in views I'm going to create a new folder called users.
‫And anything to do with users or authentication is going to go in here.
‫So let's create.
‫Create dot view dot PHP and let's also create login dot view dot PHP.
‫All right.
‫So now those should just go to blank pages.
‫Yep.
‫So it's loading the view but there's nothing in it.
‫Now we're going to include.
‫Let's see.
‫What are we going to include here?
‫Let's we'll grab the head and the nav bar.
‫So from one of these other.
‫Just go to the home here.
‫I'm going to grab just the head and the nav bar.
‫We don't want the top banner.
‫I mean, you can if you want, but.
‫So I'm going to paste that in both.
‫Create and log in.
‫And then let's say at the bottom we're just going to need the footer.
‫So we'll grab that.
‫And in both login and create we'll have the footer.
‫Now we just need the content in the middle.
‫So I'm going to go to my template or my my theme, which I should have opened in Sublime Text somewhere.
‫Let's see.
‫Yeah.
‫So let's go to the theme and go to Login.html.
‫And then we want this right here the login form box which ends here.
‫So I'm going to just grab that for log in.
‫Put that.
‫Right here.
‫Save it.
‫We can get rid of this comment.
‫And then for create.
‫Let's go back.
‫And let's go to register dot HTML and let's grab.
‫This.
‫Which ends here.
‫Wait.
‫No.
‫Let's see.
‫Where does that end?
‫Here.
‫Yeah.
‫All right.
‫So let's take that.
‫Copy that and put that right in here.
‫Save it.
‫Now let's go to login.
‫So that works.
‫Register that works.
‫Now we do have these links here like already have an account log in which you can see goes to login
‫HTML.
‫So I just want to change those.
‫So already have an account change that to.
‫Slash auth slash login and then in the create or in the login rather.
‫Change this to.
‫Slash auth slash register.
‫So now if we reload this, we should be able to use these links to go back and forth.
‫Now in the next video, we're going to work on the actual registration.
‫So the create method shows the form.
‫But when we submit it it's going to go to a store method just like we did with listings.