Hands-on with token-based authentication

The crux of token-based authentication is as follows:

  • Client requests access with a username and password
  • Application validates credentials
  • Application provides a signed token to the client

The following code example demonstrates the console cURL REST-like request for the customer user:

curl -X POST "http://magento2.ce/rest/V1/integration/customer/token"\
    -H "Content-Type:application/json"\
    -d '{"username":"john@change.me", "password":"abc123"}'

The following code example demonstrates the PHP cURL REST-like request for the customer user:

$data = array('username' => 'john@change.me', 'password' => 'abc123'); $data_string = json_encode($data); $ch = curl_init('http://magento2.ce/rest/V1/integration /customer/token'); ...

Get Magento 2 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.