Woocommerce is the next generation open source digital commerce platform. Woocommerce2.6+ is the way to access the REST API. It is fully integrated with the WordPress REST API. This allows data to be created, read, updated, and deleted using requests in JSON format. The current REST API integration version is v1
which takes a first-order position in endpoints. While we were developing the app then REST API was not upgraded. We face too much problems to create user. We make call to create user from WordPress Documentation. But now REST API has been upgraded. Now REST API have call to create user. This new release offers unmatched flexibility to bring your commerce vision to life. It features a modular code base that enables easier customization, faster time-to-market, and greater deployment flexibility to public or private cloud environments. This article explains the essential steps to authenticate woocommerce app user.
First of all set up the WordPress JWT Authentication plugin on the server side to enable customer authentication. This plugin set up is an essential step for woocommerce user authentication.
To login user, first of all, create user with WP REST API. When the user is created then API returns the token for that particular user. It has more secure login process for a user to enable access to Woocommerce REST API. The crux of login is as follows:
- User requests access with a username and password
- Application validates credentials
- The application provides a signed token to the user.
Following are the steps to get the token and how to use it in API calls.
Step by step guide to authenticate Woocommerce app user
1. Request a token from the WordPress token service
Create a post call with header and body. In header pass the content type you want to send and in body username and password. We pass username in the body rather than email because WordPress authenticate the user by username.
Below API is used to get token:
POST "http://your-host-name/wp-json/jwt-auth/v1/token"
with parameters
-H "Content-Type: application/json" -d '{"username":"<user_name>","password": "<password>"}'
2. Receive Response with an authentication token from the token service
A successful request returns a response body with the token. For example:
{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC93b29jb21tZXJjZS5pcHJhZ21hdGVjaC5jb20iLCJpYXQiOjE0NjU2MzAxODYsIm5iZiI6MTQ2NTYzMDE4NiwiZXhwIjoxNDY2MjM0OTg2LCJkYXRhIjp7InVzZXIiOnsiaWQiOiI2In19fQ.GmcVQKssY6dLS_QXTHgxVITrJR3LAqp6Y0zuCTPSW2c", "user_email":"[email protected]", "user_nicename":"test", "user_display_name":"test"}
This signed token acts like an electronic key that lets the user to login into an app. When make web API calls, supply this token to prove user’s identity.
3. Retrieve User details
Use the below API to retrieve the details of above created user
GET http://your-host-name/wc-api/v3/customers/email/[email protected]
It will return the following response with the details of user.
{"customer":{"id":6,"created_at":"2016-04-13T11:39:09Z","email":"[email protected]","first_name":"test","last_name":"test","username":"test","role":"customer","last_order_id":"713","last_order_date":"2016-06-08T05:59:22Z","orders_count":1,"total_spent":"10.00","avatar_url":"http://0.gravatar.com/avatar/?s=96","billing_address":{"first_name":"test","last_name":"test","company":"","address_1":"Raj","address_2":"","city":"Sgnr","state":"","postcode":"335001","country":"India","email":"[email protected]","phone":"1234567890"},"shipping_address":{"first_name":"test","last_name":"test","company":"","address_1":"Raj","address_2":"","city":"","state":"","postcode":"","country":"India"}}}
After completing the above steps users are enabled to order any item and they can make a call to any API for which they are authorized to.
- Benefits of Woocommerce REST API
- Introduces new product attribute endpoints (GET, POST, PUT, and DELETE).
- Includes category thumbnails on the requests for product/categories.
- Involves new endpoints to get all product orders.
Conclusion
Woocommerce2.6+ has a great scalability with more flexible architecture . These login steps not only give easy access to the Woocommerce Rest API but also improve the security of the e-commerce store. These essential steps to authenticate woocommerce app user helps the developer to make a call for REST API. You can use these steps to your Woocommerce app user. Below are some apps in the market that use the above steps to login user
- WooShopee – Woocommerce App – A smart app that follow these login steps.
- WooSeller
Feel free to contact us regarding technical or any other help to setup your eCommerce store.
References
Woocommerce– Introduction to Subscriptions Developer Documentation.
Woocommerce REST API – Introduction to Woocommerce2.6+ api .