Coding

Laravel 5.2 PHP Build a social network – Logging users in

  • 00:00:01 welcome back to the next part of my
  • 00:00:03 laravel series here in the last part we
  • 00:00:06 actually created the logic to sign up
  • 00:00:09 users so to create a user in our
  • 00:00:11 database and to to handle our signup
  • 00:00:15 requests we sent through this form here
  • 00:00:18 so the first step to user authentication
  • 00:00:23 or to you to our user management system
  • 00:00:25 now in this video we're going to handle
  • 00:00:28 the user login so if they already is
  • 00:00:32 signed up we want to amp also implement
  • 00:00:36 the method for a sign lab user to to
  • 00:00:39 just log in with his email as its
  • 00:00:40 password and we'll also redirect locked
  • 00:00:45 in users as well as sign-up users to a
  • 00:00:49 dashboard page currently not a very
  • 00:00:51 pretty one but to to another page but
  • 00:00:54 pretty much so let's get started
  • 00:00:56 now the first thing for our
  • 00:00:59 authentication to work is we need to
  • 00:01:01 make a little change to our user model
  • 00:01:03 so let me go to my user model file here
  • 00:01:06 and in here we need to implement the
  • 00:01:09 authentic cable contract that's a
  • 00:01:11 contract provided by the laravel
  • 00:01:13 framework which basically allows us to
  • 00:01:16 use built-in level authentication
  • 00:01:19 helpers so to say the of class if you
  • 00:01:23 want to put it that way to easily
  • 00:01:25 authenticate users so that we don't
  • 00:01:27 manually have to check if the password
  • 00:01:29 is equal and so on but we can use these
  • 00:01:32 handle helper functions provided by
  • 00:01:34 level and that's a framework is all
  • 00:01:36 about it yet right so to do this I'll
  • 00:01:39 implement implement the authentic cable
  • 00:01:43 contract and don't forget to add this
  • 00:01:46 import here so use illuminate contract
  • 00:01:49 off authenticate able and now we need to
  • 00:01:52 implement the methods that this contract
  • 00:01:55 or this interface as it would be called
  • 00:01:57 in other programming languages and needs
  • 00:02:00 us to yeah implement in this class
  • 00:02:03 thankfully we can use a nice little
  • 00:02:06 trait wearable provides us with namely
  • 00:02:10 the Authenticator bull trade now don't
  • 00:02:12 mistake that with the
  • 00:02:14 that for the contract we're implementing
  • 00:02:17 here that's basically a trade so to put
  • 00:02:20 it easily a bundle of functions we can
  • 00:02:24 import into any class we create add it
  • 00:02:28 yep basically in this case is all we
  • 00:02:32 need to four folders authenticated
  • 00:02:34 contract and this is obviously no no no
  • 00:02:39 surprise or no coincidence but instead
  • 00:02:43 that is how authentication and level
  • 00:02:45 works and how laravel makes it easy for
  • 00:02:48 us to make any of our models we of the
  • 00:02:53 models we created authentic edible just
  • 00:02:55 by implementing that contract and then
  • 00:02:58 using this trade here which lifts any
  • 00:03:01 illuminate off namespace and with those
  • 00:03:06 two changes here with these two changes
  • 00:03:09 we're done that's all we need to be able
  • 00:03:11 to authenticate our user now we could
  • 00:03:14 have used any model here we could have
  • 00:03:16 had a dog modeler dot PHP file with a
  • 00:03:19 dog model and a dog's database table we
  • 00:03:23 would just have to implement this
  • 00:03:26 contract and then implement or import
  • 00:03:29 the straight here this authentic adbul
  • 00:03:31 trade and we would be able to yeah to
  • 00:03:35 use the dark class as our user class if
  • 00:03:39 we would have used another name but user
  • 00:03:41 there would have been one other change
  • 00:03:44 necessary that is in our config folder
  • 00:03:46 in the app PHP file here we got where is
  • 00:03:53 it
  • 00:03:54 now it's an hour off PHP file it changed
  • 00:03:58 in layer five to two here we got this
  • 00:04:01 providers area here of the users with
  • 00:04:05 the driver of eloquent which is driver
  • 00:04:08 we use here because eloquent is levels
  • 00:04:10 over m so the way to easily access
  • 00:04:13 database tales behind our models and
  • 00:04:17 here we specify the model used for
  • 00:04:20 authentication when we use the office
  • 00:04:24 aid we will use that in a second to yet
  • 00:04:27 to access
  • 00:04:27 those authentication helper methods
  • 00:04:30 level offers us so it is by default set
  • 00:04:33 to user in the app namespace which is
  • 00:04:36 exactly what we have here app folder
  • 00:04:39 user is in the namespace app so this
  • 00:04:42 works out of the box but if you would
  • 00:04:44 have dog well we would have to rename
  • 00:04:47 this to dog otherwise it would be
  • 00:04:49 looking for a user model with the
  • 00:04:50 authenticate able contract implemented
  • 00:04:53 and we're to find one because our dog is
  • 00:04:55 the class doing this so that's important
  • 00:04:58 but in this case everything is working
  • 00:05:00 just wanted to let you know that there
  • 00:05:03 is this additional change you would have
  • 00:05:05 to do if you're not using a user class
  • 00:05:08 okay now for the talking the user is now
  • 00:05:11 prepared let's go back to our user
  • 00:05:14 controller now the first thing is I want
  • 00:05:18 you automatically redirect user to the
  • 00:05:20 dashboard if he signs up because then we
  • 00:05:22 know yeah he is authenticated right he
  • 00:05:25 is locked in so in this case I want to
  • 00:05:30 create a new few here which I call
  • 00:05:34 dashboard dash board put plate took PHP
  • 00:05:40 and I'll just say add an h1 tag here the
  • 00:05:47 dashboard we're gonna fill this with
  • 00:05:49 life later on and well we're not
  • 00:05:53 redirecting back here anymore but
  • 00:05:55 instead I want to basically redirect to
  • 00:06:02 another route I would have to create
  • 00:06:03 which is called dashboard and let me
  • 00:06:07 create this route here real quick so
  • 00:06:09 again here in our middleware I'm going
  • 00:06:12 to create a getter out here route get
  • 00:06:17 dashboard
  • 00:06:21 so we'll also use a controller
  • 00:06:23 functional I the user oops
  • 00:06:28 user controller at get dashboard and
  • 00:06:34 name will be as I just used it dashboard
  • 00:06:39 don't forget a cell a semicolon so this
  • 00:06:42 is prepared now back here I'll create a
  • 00:06:45 new function at the top which I just
  • 00:06:47 call public function get dashboard and
  • 00:06:52 here all I do is return a few namely
  • 00:06:56 that dashboard few H has created so we
  • 00:06:59 can try this out this should already
  • 00:07:01 work if I enter and you name here like
  • 00:07:06 yeah let's use test so we need a unit
  • 00:07:09 receiver so let's use test at gmail.com
  • 00:07:14 my name first name is test and the
  • 00:07:16 passwords also test so we're creative
  • 00:07:19 well we got forward to the dashboard
  • 00:07:21 great now let's create our side in logic
  • 00:07:26 that we want to execute when we try to
  • 00:07:28 sign in here first we will try to route
  • 00:07:32 we need for this so let's start our
  • 00:07:35 welcome view and hook this action here
  • 00:07:37 up to it route which we still have to
  • 00:07:40 create a route called sign in in our
  • 00:07:44 routes folder just copying this sign up
  • 00:07:48 route here really quick and then I will
  • 00:07:50 just rename it to sign in in all three
  • 00:07:54 places here so this is our sign in route
  • 00:08:01 which we set to use our post sign-in
  • 00:08:04 function here and in this function we're
  • 00:08:07 now using the office aide which is
  • 00:08:10 another one of those shortcuts lateral
  • 00:08:12 ships with which allows us to easily
  • 00:08:14 access our authentication helper methods
  • 00:08:19 we access this helper method or these
  • 00:08:22 helper methods floor office aide which
  • 00:08:25 we import form from illuminate support
  • 00:08:28 facades then see off the side
  • 00:08:31 and here we got this function attempt of
  • 00:08:34 attempt we'll try to log in a user with
  • 00:08:38 the credentials we provide here as
  • 00:08:40 argument which will be an array of your
  • 00:08:43 arguments or of credentials and if the
  • 00:08:46 attempt fails this will returns false
  • 00:08:49 and if it succeeds it will return true
  • 00:08:51 so we can basically just check this in F
  • 00:08:54 condition here if off attempt so if it
  • 00:08:58 is successful then we want to return our
  • 00:09:02 redirect to our dashboard route and if
  • 00:09:08 it fails well we just want to return a
  • 00:09:11 redirect back so back to the front end
  • 00:09:15 screen to the starting screen so to say
  • 00:09:18 to our welcome view now here I'll pass
  • 00:09:21 an array and we want to authenticate
  • 00:09:24 using our email which will be stored I
  • 00:09:28 will request in which you can access
  • 00:09:29 through the email name because again
  • 00:09:33 this is here now welcome few what we use
  • 00:09:36 for an a as a name for our input field
  • 00:09:39 when we input the email and here for a
  • 00:09:42 password we're using the name password
  • 00:09:44 and therefore in my user controller oops
  • 00:09:48 I have my second parameter which is the
  • 00:09:53 password obviously which I can access
  • 00:09:57 through the password key in my request
  • 00:10:00 array here so we can almost try out if
  • 00:10:04 this voice but we've got to do seven
  • 00:10:07 more things we have to add our session
  • 00:10:11 token here in the login form as well
  • 00:10:14 this is basically the case for every
  • 00:10:16 form we're posting in Larra well we
  • 00:10:19 always have to include this session
  • 00:10:22 children otherwise we'll always get this
  • 00:10:24 tope mismatch error message so now let's
  • 00:10:28 try this out I will create a new user
  • 00:10:30 here test at yahoo.com named test
  • 00:10:34 password test so you see a very creative
  • 00:10:37 when it comes to the names of my testing
  • 00:10:38 users here so yeah that works obviously
  • 00:10:41 now let's use let's use them here
  • 00:10:44 ooh password of task 8 works too so we
  • 00:10:48 can see in our user controller our
  • 00:10:51 authentication attempt through this off
  • 00:10:54 helper classroom through this office
  • 00:10:57 aide and a helper class attempt here the
  • 00:10:59 AL helper function was successful it
  • 00:11:01 returned true and therefore we were
  • 00:11:04 forwarded to our dashboard view or our
  • 00:11:07 dashboard route which again returns this
  • 00:11:10 view here now let's try something wrong
  • 00:11:13 let's try
  • 00:11:14 yeah the RAM a random password here and
  • 00:11:17 you see we're just taking back to our
  • 00:11:19 welcome view and this is exactly the
  • 00:11:22 behavior we want here so this works now
  • 00:11:26 one more thing
  • 00:11:26 let's also log in the user when he signs
  • 00:11:29 up because yeah we are redirecting to
  • 00:11:32 our dashboard but currently we're not
  • 00:11:34 really protecting our dashboard we could
  • 00:11:37 just access our dashboard by typing this
  • 00:11:41 right so it's not something we have to
  • 00:11:44 login and we will focus on a logic to
  • 00:11:48 protect our routes soon but for now
  • 00:11:51 let's at least login our user so that in
  • 00:11:54 theory he would be locked in we can
  • 00:11:56 access another helper method here the
  • 00:12:00 login method where we just pass our user
  • 00:12:02 the user which has created and this will
  • 00:12:05 automatically login the user so let's
  • 00:12:07 again try this and I'll be a little bit
  • 00:12:10 more creative now I'll use Chris at
  • 00:12:13 gmail.com
  • 00:12:14 I guess this email address exists but
  • 00:12:17 does really matter here and he has a
  • 00:12:20 password of Chris said mintus and we're
  • 00:12:24 still forward we're not getting any
  • 00:12:26 error so this works otherwise would have
  • 00:12:28 got an error and now we're technically
  • 00:12:32 locked in not just forwarded but also
  • 00:12:34 technically logged in so this is sign up
  • 00:12:37 and sign in and now that we got this
  • 00:12:40 ready we'll have chance to focus on
  • 00:12:44 additional steps for example we want to
  • 00:12:46 validate that we don't have double email
  • 00:12:49 addresses in our user database right
  • 00:12:53 that will be something we'll have to
  • 00:12:55 and that we finally also want to create
  • 00:12:57 our dashboard and the possibility to
  • 00:12:59 create posts and see our peoples posts
  • 00:13:02 we'll do this in the next videos see
  • 00:13:04 that bye