Okay, now we've done as much work as possible to make it easy to integrate a new service, let's roll through the entire flow of adding a new provider. And we're going to do GitHub. Okay, so first things first, let's head over to our social index file here. And let's create
00:16
our new link for GitHub. And we know that our link now to redirect works with any service. So we can just add in GitHub there. And we should be just redirected over to GitHub nice and easy. Now, this is course isn't going to work at the moment, because we don't have any credentials for GitHub. So the first thing we'll do is go ahead and set up a new app. So as far as I'm aware,
00:39
you need an organization for this. But let's head over to any organization that we have and hit developer set settings and our auth apps. So we're going to go ahead and register an application here, we are going to call this Laravel socialite or socialite screencast to keep it the same, we'll give a homepage URL. So that will just be the URL here. And of course, we'll give our
01:03
callback URL, which we know is auth slash callback, and then it will be slash GitHub. So we're going to need to register that. Okay, so let's register this application. It's nice and easy. And we now have our client ID. And we can generate a client secret. So let's grab this. Let's go over to our EMV file, let's create our a GitHub client ID and paste that in, we'll go over and generate a client
01:28
secret, we'll copy it, we'll go back over, and we'll create a GitHub client secret value. And then we'll grab the URL here, paste this down, change this to GitHub, and of course, change that to GitHub. So we're done with our EMV values. So now we've got our credentials, we're going to need to add them to config and services. So let's basically copy the Twitter value here,
01:54
paste it down and just change this over to GitHub. So we'll change each of these to GitHub. And that's it, we should now be able to redirect. And let's just check over on here with the permissions that we are allowing. So let's go down, I don't think we need to do this here. So let's go straight over and just click on this and see what it gives us. Okay, great. So
02:19
personal user data, email address read only, that looks good. Now before we go through and actually redirect, let's set up everything else that we need to do to create that user in our database. So the first thing is going to be a GitHub ID. So let's go and create a migration to store this. So make migration, and we're going to add a GitHub ID to the users table. So let's go add
02:45
GitHub ID to users table. And we'll go ahead and do exactly what we did before, create out a string, that's going to be the GitHub ID. And of course, that is going to be nullable. So let's go ahead and run our migrations here. Not forgetting if we're using fillable to go ahead and add this also to our model. So we can now add that to our user. And now we just need to figure out how we're going
03:11
to create a GitHub user. So we're going to take this create X user class, and we can just copy and paste it because it's pretty much going to be the same thing. And we can create a create GitHub user class, paste this in and just change around the implementation for creating a user back from GitHub. So we're going to say create GitHub user. And here is the only thing that
03:34
really changes is the GitHub ID just storing the GitHub ID. So that's the only change that we're doing. But if you needed to store additional information specifically for a GitHub user, of course, you can now fill in anything you need here. And that will only work for a GitHub user. Okay, so now that we've done this, we're going to head over to our factory,
03:53
our create user factory. And we're just going to do the same thing here. And for GitHub, when we resolve that, we want to create GitHub user instance. And that is all we need to do. So add the config, obviously, create the app on the service, add the stuff to the database that we need, and then create a new class that's responsible for creating a GitHub user. And
04:15
everything will now work because in our callback controller, GitHub will now come through, it will pick the class that we want to create the GitHub user, and it will create that. Okay, let's try this out. And hopefully, we've done everything right. Okay, so I'm going to go and sign in with GitHub here, I'm actually going to clear the database out before we start here,
04:33
just so we are completely clear, sign in with GitHub. And we'll go ahead and authorize this. And we should be redirected back. And of course, we are now signed in with the details. And of course, that's going to work in exactly the same way. When we sign back out, and we log back in with GitHub, and that's just going to redirect us back to the app. Nice.
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.