This episode is for members only

Sign up to access "Social Authentication with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
08. Using a factory to create users

Transcript

00:00
OK, so rather than return a string here, we actually want this to return a usable class that we can use to create either a Twitter user, a GitHub user,
00:07
or any of the other services we use. So let's think about the structure of this, and more importantly, the contract, which will make all of these things
00:15
pretty much do the same thing. So over in App, we're going to create out an Actions folder. So these are just things that do one job. And in here, we're going to create a social directory.
00:26
And in here, we're going to create a new file called CreateXUser. That is what we're going to return from this factory to create a user.
00:35
So let's go ahead and just scaffold this out. So we'll give this a namespace here of Actions and Social. This is CreateXUser. Let's think about the API here.
00:47
What do we want to have in here? Well, if we return back to our callback controller, once we've selected a service, we probably want to create a user.
00:56
And we want to pass in the user that we get back from Socialite, because that contains all of the information about that user. So that's how we want it to look and work.
01:05
And then Create needs to return a user to us, so we can pass this directly into AuthLogin and have that user logged in. So let's go over and create out a contracts directory.
01:17
And we'll create an interface that's common between all of these actions. So we will call this CreateXUser. And let's go ahead and just add in the namespace here,
01:28
Actions and Social and Contracts. And this is going to be an interface called CreateUser. And in here, we can define that create method. So we can say that we want this to have a create method.
01:42
We want it to return a user model. And we want to accept into here a user from Socialite. Now, I'm not going to type in the user here to the actual implementation, because it
01:56
could be OAuth 1 or 2. And then we'll get two different things back. You could type in this and have two options. But I'm just going to leave it how it is for now.
02:05
But we really importantly want to make sure this returns a user. OK, so now we've got this interface. Over in our CreateXUser, we can go ahead and implement
02:14
this CreateUser interface. And you can see that my editor is telling me here that I need to implement a method. So that is going to be that create method.
02:23
So let's create that out, make sure that we return a user from this. And in here, that takes in a user. And we're done.
02:34
So now all we need to do is move the functionality for creating a XUser over to this specific class. And we know where that is. It's now OAuthCallbackController.
02:43
So let's uncomment this just temporarily. Let's grab what we have here and go ahead and comment out OAuthLogin. And let's move this over to here,
02:54
making sure that we return that newly created user. So we've basically just moved this stuff over. And now we have a class that's sole job is creating a user for our application, but with that XID
03:09
coming through here. Great. OK, so now that we've got this specific class, what we can do is head over to our factory.
03:15
And I'm just going to comment out GitHub for now. And we can return a new CreateXUser instance in here. So let's roll through everything that we've done with this factory that we've created.
03:29
And then we'll go through the flow of actually trying to sign in. OK, so our OAuthRedirectController obviously just picks the service.
03:36
This is the easy one. And the OAuthCallbackController grabs the user from that service that we are coming back from. It creates a user factory, which takes in a service.
03:47
And of course, that's going to be the service that we're passing in in the URL. And that is then going to resolve that Twitter class that we've just created and return it back to us from here.
03:58
And we know that every single class that we now create for creating a user for any service is always going to have this create method. It's always going to accept any user from socialite.
04:09
And it's always going to return to us a user. So we can use that to log in. And that's pretty much it. So createXUser will call create.
04:18
We'll get a user back like we were just doing in our controller. And then we can pass this directly through to OAuth and log in.
04:28
So let's pop that in there and indent this. And we should be done. OK, so let's try this out. We'll bring back our redirect.
04:36
And let's go over and go through the whole flow again. So I'm actually going to go and delete the user we have in the database. Hit Sign In.
04:45
Let's wait for the redirect to happen. And there we go. That worked really nicely. So it's invoked that create method inside
04:52
of that createXUser class, done everything that we need it to do. And now if we sign out and we try and sign back in, we should see exactly the same thing.
05:03
And there we go. It works nicely. So let's test out our new factory by implementing another provider.
05:10
So we're going to choose GitHub. And we're going to go through the whole flow of basically just adding a new service to our app that users can sign in with.
05:20
Now that we've got that factory and we are set up for this to work really nicely, it shouldn't be too difficult. So let's head over and do that next.
12 episodes1 hr 4 mins

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.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!