تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Sanctum Token Auth Overview0:00

Let's take a look at how we can do authentication. We'll start off with the backend in this video. So we're going to make use of token-based auth in Sanctum and incorporate that into Lighthouse. So let's take a look at how this is done traditionally in a REST API, and then we'll make use of a package that converts it to GraphQL. So if you look at the Sanctum docs here, there's an example of how to do token-based auth. So if you scroll down here, you'll see some code. So we're validating the input. So that's the email, password, and device name. We're checking for that User, and then we try to log them in. If it's not successful, then we throw this new ValidationException.

We're checking for that User, and then we try to log them in. If it's not successful, then we throw this new ValidationException. But if it is successful, then we create a new token here. And we can use this token for additional requests for any endpoints that are behind the auth:sanctum middleware. So for example, in our routes/api.php. So let's open up that file. There's a default endpoint here that's behind the auth:sanctum middleware. And if you pass the correct VAR token, it will return the logged in User. So let's take a look at an example here. So I have the backend from my React Native project, and you can see the endpoint for logging in here.

Testing REST Login Token1:02

So let's take a look at an example here. So I have the backend from my React Native project, and you can see the endpoint for logging in here. It's AuthController@store. So let's go to that. store should be down here. And it's basically the same thing as the code in the documentation. We have some validation. We grab the User. And here is where we're returning the token. So let's take a look at this in our REST client. So I'm hitting this endpoint with these fields, and there is a User with this email. So let's hit that endpoint.

So I'm hitting this endpoint with these fields, and there is a User with this email. So let's hit that endpoint. We get this token back. So let's grab this, and we have this endpoint here, API tweets, which is behind the auth: sanctum middleware. So right here, in order to get the tweets, we have to be a logged in User. So if we take a look at our headers here, I'm already passing it here, but if I don't pass it, we'll get an error. So let's go ahead and pass this Authorization token. Let's paste in the token we got from the logged in User.

Adding GraphQL Login Mutation1:56

So let's go ahead and pass this authorization token. Let's paste in the token we got from the logged in user. And if I did that correctly, we should get the tweets. And we do. Cool. So we want to do something similar in GraphQL. But before we install the package, let's see if we can do it manually first. Go back to our other project. Let's go to our schema. And let's add a new mutation for logging in. So in our mutations, let's add a new one here called login. loginTest, actually, because the package is going to have one named login.

So in our mutations, let's add a new one here called login. Login test, actually, because the package is going to have one named login. And this will take in an email. That's a string. It's going to take in a password. That's also a string. Actually, it should be required. Required, required. And also a deviceName, which is also a string.

And also a device name, which is also a string. And what are we returning here? So the token is just a string, but we actually want to create a new type. Let's name it AccessTokenTest. And that's required. And we're going to make use of custom resolvers here. So let's grab this field and we'll make one in a second. We'll name it LoginTestResolver.

We'll name it LoginTestResolver. LoginTestResolver. Okay. But before we do that, let's define our new type here. So type AccessTokenTest. And all that does is, or the only field it has is a token. And that's a string that's required. Okay. So let's go ahead and create this custom resolver called LoginTestResolver. Should be php artisan lighthouse mutation, I believe.

So let's go ahead and create this custom resolver called LoginTestResolver. Should be php artisan lighthouse:mutation, I believe. And LoginTestResolver. Okay. Let's go to that. LoginTestResolver. Okay. And let's just grab the code from the Sanctum documentation. We'll skip validation. I just want to see the token returned when we try it out.

We'll skip validation. I just want to see the token returned when we try it out. So we'll grab all of this. Let's paste it in here. Let's save that. Let me just reinvent this. And let's import what we need here. So User. We need Hash. We need ValidationException.

We need hash. We need validation exception. Okay. And for these, we can grab the programs from this arcs array. So let's replace this with arcs email. arcs email. And let's replace the others as well. So this one will be arcs password. Okay. And this one will be arcs device name.

Okay. And this one will be arc device name. And we also want to make sure that we return an array here with the key of token. So I'm going to grab this. Let's make it an array, say token. And we'll paste that in. Okay. So hopefully I did that right. Let's grab a User here and try to make use of this mutation. So I'm going to go into my database here.

Let's grab a User here and try to make use of this mutation. So I'm going to go into my database here. Let's grab a User. Let's grab the first one here. Okay. And let's go into GraphQL playground and try that out. Let me just refresh this. Let's try that new mutation out. So it's a mutation. It's loginTest, I believe.

So it's a mutation. It's login test, I believe. And we have an email, which is this. The password is password. And the device name can be anything. Let's just say mobile here. Okay. And that should return a token. Let's see if this works. Okay, so it does.

Configuring Sanctum Guard5:46

Let's see if this works. Okay, so it does. We do have a token, which I will copy. And now let's go back to our code. And let's make one of these queries use the guard directive, which is the equivalent of the auth sanctum middleware. So back here, let's use the guard directive on this one here. Actually, before I do that, let me just comment this out. Let's bring this back. So now there's no guard.

And it says server cannot be reached. Let me just refresh. Let's try that again. And we are still getting unauthenticated. So I think that's because we have to change the guard in our lighthouse config. So let's go to lighthouse. And if we check out the guard here, where is it? Right here, the authentication guard. The default is API, but we are making use of Sanctum here. Sanctum.

The default is API, but we are making use of Sanctum here. Sanctum. Let's try that out. Let's run this again. And now we are getting the users. So I'm going to put this user's endpoint back. So back to our schema. Let's remove the guard from here. And I'm going to make a new one here called me, which will return a user.

And I'm going to make a new one here called me, which will return a User. And we'll put the guard directive on this. But we'll also use the auth directive. And all this does is return the logged in User. So let's try that out. Let me refresh here. Let's make use of that new query called me. me. And let's just say user.

Me. And let's just say User. Sorry, not User. Name. And email. Okay. So that works because we're logged in. If this were not correct, we should get an error. And we do. Cool.

Installing Sanctum GraphQL Package8:08

And we do. Cool. So now we have a functionality like logging in, logging out, registering, forgot password. This package converts it into custom resolvers like I just showed you for login. So let's go ahead and make use of this. So let's install this. Let's publish the config and schema. So this created a new sanctum.graphql schema,

Let's publish the config and schema. So this created a new sanctum.graphql schema, which should have all of the queries and mutations for all the auth stuff. So let's take a look at that. It's called sanctum.graphql. sanctum.graphql. Okay. And you can see that there is a User type, but we already have one. So I'll comment this out. You can see the access token, like we just did for our login.

So I'll comment this out. You can see the access token, like we just did for our login. And down here, you'll see the login, which has a custom resolver. Right here. So it's very similar to what we did. Okay. So let me save that. What's next? So we have to import this sanctum.graphql in our main schema. So we can do that like this.

So we have to import this sanctum.graphql in our main schema. So we can do that like this. It looks like a comment, but that should import it correctly. So back to our schema. It's in the same folder. So that should work. So all the way down here, let's import that. Okay. What's next? We have to add this hasApiTokens contract on the User model.

What's next? We have to add this hasApiTokens contract on the User model. Let's do that. So let's grab this. Let's go to our User model. Let's paste that in. And we also have to implement that. Let's grab this and let's paste it in here. Okay. Anything else?

Okay. Anything else? Okay. For our sanctum config, it says we have to have an empty stateful array. So let's do that. sanctum. stateful. Right here. Let's just comment this out. And we'll paste in that empty array.

Let's just comment this out. And we'll paste in that empty array. Okay. Is that all? Make sure the middleware is enabled for Lighthouse. So attempt auth in Lighthouse. Attempt auth. Okay. So it is. Okay.

Testing Auth Mutations10:22

So it is. Okay. So let's try this out. So there's examples for all of the auth functionality. Let's try logging in and logging out in GraphQL playground here. So let's comment this out for now. Let's try out the new functionality for logging in. Let's get rid of this. Say mutation. Actually, I might have to refresh.

Say mutation. Actually, I might have to refresh. Say login. Okay. And that's of type login input. So that should be an object. We have, or sorry, that should be input. And that should be an object. There we go. We have email.

There we go. We have email. So again, let me just paste in that email I copied. Okay. And the password should be password. Okay. And you can see we get this token as a return type. So let's try that. Let's run this mutation. And we do get a token.

Let's run this mutation. And we do get a token. So let me grab this. Let's replace this token here. Let's comment this out for now. Let's try out our me query. And this should work if I did it correctly. And it does. Cool. Let's try logging out.

Cool. Let's try logging out. So that should delete the token. So let's say mutation should be called logOut. Okay. There should be no params. Let's just grab the message here. Okay. And now if we try that me query again, we should get an error because the token has been deleted. Try this.

And now if we try that query again, we should get an error because the token has been deleted. Try this. And we do. Cool. Let's try one more for registering, which creates a new User. And I believe it returns the token as well. So we can log in after we register. So let's try that out. mutation register. Okay.

Mutation register. Okay. And you can see the input has name, email, password, and password confirmation. So let me just type that in behind the scenes. Okay. So I typed in all the args. Check out the token here. So this should create a new User and also return the token. Okay. So we're getting this error because I have this query running up here.

Okay. So we're getting this error because I have this query running up here. So let's just comment that out. Let's run this again. And hopefully we have this new User. And we do. We get this token as well, which you can use to log in. But I just want to check if the User was created. And there he is. Cool.

And there he is. Cool. And for all the other auth functionality, like forgot and reset password, you can check out the documentation on how to do that. So, yeah, that's off on the back end. It's basically what you would do in REST, but converted over to GraphQL. So let's go ahead and make a commit. And as always, git add, git commit. Let's say auth in Lighthouse.

Token based auth in a REST APIConvert login to GraphQL mutationConvert auth to GraphQL

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟