Hello Laravel Friends,

In today’s blog, I will explain How to Create User Register and Login GraphQL API in Laravel.

GraphQL is a powerful query language for APIs that provides a flexible and efficient way to retrieve and manipulate data. In this tutorial, we’ll explore how to create a user register and login GraphQL API using the Laravel framework. Before that, learn How to Create GraphQL API in Laravel?

By the end of this tutorial, you’ll have a working GraphQL API that allows users to register and log in to your application.

Steps to Create User Register & Login GraphQL API in Laravel

Create Project and Install GraphQL Package

Step 1: Create a Laravel project using the below command

Step 2: Install the GraphQL package via composer using the below command 

Step 3: Publish the configuration file using the below command

After running this command, the graphql.php file will be created in your config folder

Step 4: After that, run the migration command to add a users table in your database

Register User API

Step 5:  Now, we will create a type to use for the GraphQL API. 

You will find this type on this path in your Laravel project – App\GraphQL\Types

Now add the below code in your type file. 

Step 6:  Now, we will create a mutation to use for the GraphQL API. 

You will find this type on this path in your Laravel project – App\GraphQL\Mutations

Now add the below code to your mutation file 

Step 7: Now configure both your types in the graphql.php file like the below code 

After that, clear the config cache using the below command

Step 8:  Now open Postman

register user API

Output –

Login User API

For Login, we have to use the Laravel Sanctum package.

Intro laravel-sanctum : 

Laravel Sanctum is provided for token-based authorization to SPA(single page application) like mobile apps and React web apps. It provides multiple API tokens to its associative user. The token provides the ability to access restricted APIs/platforms.

On the web, we can use a session to authenticate. But in headless APIs, we need to use token-based authorization.

First, you have to check in your composer.json file whether the laravel-sanctum composer is already installed or not. If not, you have to follow the below steps to configure the sanctum composer

Step 9: Install the sanctum via composer 

Step 10: Publish the configuration file using the below command

You should publish the Sanctum configuration and migration files using the vendor:publish Artisan command. The sanctum configuration file will be placed in your application’s config directory.

Sanctum will create one database table to store API tokens, so run the migration command.

Step 11: Create login user graphql type 

And add the below code in LoginUser File.

And add the below code in User File.

Step 12: Create login user graphql mutation

And add the below code to LoginUser File.

Step 13: Add type and mutation in the graphql.php file

And clear the config cache. 

Step 14: Now open Postman

login user API

Output – 

Here a token is generated now; you can use this token for any after login process.

Conclusion: 

In this tutorial, we’ve learned how to create a user register and login GraphQL API in Laravel. If you have any doubts, share them with me through the comment section. Stay in touch with us to learn more about Laravel.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]