So if you made it past the last episode, I have really good news, that's probably the hardest part about this entire process. Actually creating the app with the service that you want and grabbing
00:09
the right keys, setting up the callback, giving the terms and conditions depending on what data you're trying to access, that is the hardest part. Now this part is going to be relatively easy compared to that because all we want to do is extract the data once we get back to this screen here. So what we're going to do is head over to our auth callback controller and in here just
00:29
grab the data for that user. Now to do that again what we use is the socialite facade and for this we go ahead and again choose the driver, at the moment we're just working with x or twitter, and then all we do is use the user method. That's all we need to do, we don't need to pass anything in here from the request, there's nothing complicated about this. So let's go and just
00:52
die dump on what we get back here, and we might see an issue with refreshing this but let's try it anyway, and there we go, great. So this is now giving us all of the information from our user that we can now use to insert into the database. So let's take a look at what we've got here, we've got an id, so that's the id of the user over on the service that you're working with,
01:14
so that'll be something like twitter or github, the ones that we are using, we'll have a nickname, potentially a name, an email, these will vary obviously between services but we do have a common way to extract this information, we'll take a look at that in just a second, and of course we've got the avatar and then we've got a bunch of other information here as well. Okay so what we're
01:35
going to do is actually dive into the socialite code and just have a look at this, but first of all we want to know what we're actually getting back here, so we've got socialite one which is one and user, so let's go ahead and open up this socialite source one user class and let's go ahead and look at this abstract user, so I'm going to open this up and let's take a look at some of the
01:55
methods that we've got in here, so you can see that we've got all of the properties here which is pretty obvious and then we've got some common things between all of the services that we're going to use, so we've got the get id method which just gives us back our id, we've got get nickname, get name, get email, so all of the stuff that we need to actually create a user including the
02:16
avatar if we want to use that as well, or if you just want to get the raw data there's a method on there to grab that, so basically from our code here we can say something like get email and we'll go ahead and just dump out the user's email onto the page, now at the moment this isn't working because we have already been redirected back, we'd have to go through the whole flow again,
02:36
but that's pretty much how to grab the user, it's very very simple in comparison to actually setting everything up, so now that we've got the user we of course want to do something with this and we want to create the account in the database, but first we need to do a little bit of setup to our app to allow specific things to be stored, so let's head over to the next episode and do that.
12 episodes•1 hr 4 mins•1 year ago
Overview
Need to add social authentication to your Laravel apps? It’s almost zero effort using Laravel Socialite.
We start with the basics, add authentication with one provider, then use a design pattern to make adding additional services a breeze.