Drupal Commerce Login and register API

Here we are going to configure and use login and register Rest API’s. So for Drupal commerce we can use the Drupal Core Restful web service. Make  sure you have installed the Core Restful web service module and other related module as below.

Here Rest UI is a contributed module.

Go to Configuration -> Web services -> Rest

Enable below resources which are available in Core Restful web service module.

Make sure we are not using basic authentication for Register resource, always use cookie authentication, 

Customer Login API

Below provided request details for  customer login. Login API is in built to Drupal core,  so no need of any resource to be enabled.

Below provided the request details.

URL- http://your-domain/user/login?_format=json

Request Body:  {

  “name”: “admin”,

  “pass”: “admin123”

}

No need for Authentication header.

So you can see the response below for successful login.  Uid is the user id of the customer.

{
"current_user": {
"uid": "1",
"roles": [
  "authenticated",
  "administrator"
],
"name": "admin"
},
"csrf_token": "5-cUDTlyN0SfxyWKPvsppTy6IEUVxBoAaSrXtbmkt2M",
"logout_token": "TxuSUqi0Y9m1Cvp3WnoVj8_1fsaBCTerwH_NdOexCjE"
}

you can use logout_token for logging out the user as below get request

While using user logout API it gives a blank response with 204 status
http://site.com/user/logout?_format=json&token=TxuSUqi0Y9m1Cvp3WnoVj8_1fsaBCTerwH_NdOexCjE

it gives a blank response with 204 status.

Customer Register API

Here we are going to register an anonymous user using Rest API

Make sure the anonymous user role has below permission in People->Permissions

Call the API as provided below.

Request URL- http://yourdomain/user/register?_format=json

Method  POST

Request BodyBody Raw text

{
  
  "name": [
    {
      "value": "sample user"
    }
  ],
  "mail": [
    {
      "value": "test@gmail.com"
    }
  ],
  "pass": [
    {
      "value": "test123"
    }
  ]
}

Get the session token from below which we need to use as a x-csrf token for anonymous user.

Get Request – http://localhost/dnstore/web/session/token?_format=json

No need of any headers, this will provide session token of anonymous user as a plain text, this we need to pass in headers as x-csrf-token as provided below in headers.

Headers

After submission you will receive response as below.

{
    "uid": [
        {
            "value": 5
        }
    ],
    "uuid": [
        {
            "value": "75ef4f86-24ec-4658-b172-4dce50d68746"
        }
    ],
    "langcode": [
        {
            "value": "en"
        }
    ],
    "name": [
        {
            "value": "sample user"
        }
    ],
    "created": [
        {
            "value": "2022-08-25T07:13:22+00:00",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "changed": [
        {
            "value": "2022-08-25T07:13:22+00:00",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "default_langcode": [
        {
            "value": true
        }
    ],
    "customer_profiles": [],
    "path": [
        {
            "alias": null,
            "pid": null,
            "langcode": "en"
        }
    ],
    "commerce_remote_id": [],
    "user_picture": []
}

So here Login and register API’s provides basic details of the customer after successful login/register. We will be discussing in next article topics like how to get the users orders and how to view/update billing and shipping address of the customer.

Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...