Coding

Laravel 5.2 PHP – Build a Shopping Cart – #6 User Sign In

  • 00:00:00 so I'll come back I finished the last
  • 00:00:03 video with these sign up functionality
  • 00:00:06 in place next it's time to allow the
  • 00:00:09 user to actually sign in and to log out
  • 00:00:12 them so I want to do this so I will just
  • 00:00:15 copy the code from my sign up view here
  • 00:00:18 and paste it enter the sign-in page
  • 00:00:20 because it's pretty similar
  • 00:00:22 we still need email password for all of
  • 00:00:24 that I will change to router user dot
  • 00:00:27 sign-in even though I haven't created
  • 00:00:29 this route yet and what I also want to
  • 00:00:33 do here is to change this to sign in as
  • 00:00:36 well as this button here so with that my
  • 00:00:42 sign-in page looks looks ok so far and
  • 00:00:46 the next step is to go to the routes PHP
  • 00:00:51 file and create the appropriate routes
  • 00:00:53 now we'll just copy the sign-up routes
  • 00:00:55 here since well I can easily adjust them
  • 00:00:59 to it Hamleys sign in case so low did
  • 00:01:03 you sign up page here with the hope star
  • 00:01:06 was the wrong one loaded here with the
  • 00:01:08 get sign in action low T sign in page
  • 00:01:12 with the get sign-in page and here
  • 00:01:14 submitted of course also adjust the
  • 00:01:16 route names so that's fine
  • 00:01:20 and on the user controller I will create
  • 00:01:23 two new functions I'll create the get
  • 00:01:27 sign-in function here which will
  • 00:01:29 basically just return me the sign-in
  • 00:01:32 view so user dot sign in a staff you I
  • 00:01:35 want to return of course referring to
  • 00:01:38 this user sign and or plate or PHP file
  • 00:01:42 here and then the next is the post
  • 00:01:47 sign-in function here which will take a
  • 00:01:51 request object since it's a post request
  • 00:01:54 and here I first also want to validate
  • 00:01:58 however I'm of course not validating if
  • 00:02:01 this email address is unique because it
  • 00:02:02 will always oftentimes be if someone
  • 00:02:05 tries to sign in because he already
  • 00:02:07 created a user right but our validate
  • 00:02:10 you remaining data here and
  • 00:02:14 theoretically I could also leave out the
  • 00:02:16 validation because I am checking if the
  • 00:02:18 provider credentials match my usual
  • 00:02:23 credentials in the database
  • 00:02:25 anyways so invalid data would never be a
  • 00:02:28 problem here but I still want you
  • 00:02:31 provide nice error messages in the case
  • 00:02:33 of invalidate or to the user so I'll
  • 00:02:36 leave it here but you could leave it out
  • 00:02:37 theoretically next I want to use levels
  • 00:02:41 of indication for say to try to login
  • 00:02:43 the user so I will import this visaed
  • 00:02:46 here use off allows me to do this and
  • 00:02:51 then I simply run off attempt and I want
  • 00:02:56 to pass the credentials here so I will
  • 00:02:58 pass an array where I have the email
  • 00:03:02 address with the provided email address
  • 00:03:06 on the request and the password
  • 00:03:10 referring to the of course the password
  • 00:03:13 provided in the request so this little
  • 00:03:19 try to lock the user in and it will be
  • 00:03:23 placed in an if block here to check if
  • 00:03:29 this is successful if this is successful
  • 00:03:32 I will return a redirection 2d profile
  • 00:03:36 route which I also still have to set up
  • 00:03:39 and otherwise I will return a redirect
  • 00:03:43 back so in this case validation or login
  • 00:03:49 signing it user in would fail and as
  • 00:03:51 shown in previous videos of course he
  • 00:03:54 could add error messages here and output
  • 00:03:56 them in the views but here I want to
  • 00:03:59 focus on the core logic for now so
  • 00:04:02 that's why I'm doing it this way and
  • 00:04:04 with that I'm trying to block the user
  • 00:04:06 in will see if that is successful and in
  • 00:04:10 case you are wondering how does Larry
  • 00:04:12 world know that well what the password
  • 00:04:16 is and so on well basically because we
  • 00:04:18 named it password here and therefore if
  • 00:04:20 we were to rename this in our database
  • 00:04:23 table and we would not work with the
  • 00:04:26 default password too
  • 00:04:27 level is expecting we would have to tell
  • 00:04:30 the off the Sade or level dead we were
  • 00:04:34 using a different naming convention but
  • 00:04:37 here I'm using passwords so this is all
  • 00:04:38 fine and with that I will go back to my
  • 00:04:42 sign-in page
  • 00:04:43 I already adjusted this route so I will
  • 00:04:46 go to my header then make sure that I
  • 00:04:48 hook this route here up so this should
  • 00:04:51 lead to user sign-in and I also want to
  • 00:04:55 load the bro file page so I will copy
  • 00:04:59 this just to have the skeleton get rid
  • 00:05:02 of all this form related code here and
  • 00:05:05 just output user profile here so that we
  • 00:05:08 can see that this works I'll also create
  • 00:05:11 the appropriate route and routes the PHP
  • 00:05:14 file so route get let's say user slash
  • 00:05:21 profile and I should use the user
  • 00:05:29 controller and Andy get profile action
  • 00:05:32 which I also have to create and I'll
  • 00:05:35 name this user profile so in the user
  • 00:05:39 controller I'm creating one final route
  • 00:05:42 for now or one final action to get
  • 00:05:44 profile profile action here which simply
  • 00:05:50 should return me a view namely the user
  • 00:05:53 profile view which I just filled with
  • 00:05:58 some life here in the user folder so
  • 00:06:01 with all that in place I'm going to
  • 00:06:04 reload this page I head over to the
  • 00:06:06 sign-in page and I try to sign user in
  • 00:06:09 and since I get to the user profile this
  • 00:06:13 looks pretty good
  • 00:06:14 however I'm currently not protecting
  • 00:06:17 this route I could have accessed it
  • 00:06:20 without being locked in and I don't have
  • 00:06:23 any log out functionality in place so
  • 00:06:26 I'll take care about this in the next
  • 00:06:28 video see you there bye