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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.