Skip to content

Allow using the console with cookie authentication - #75

Open
pgk wants to merge 4 commits into
Automattic:masterfrom
pgk:master
Open

Allow using the console with cookie authentication#75
pgk wants to merge 4 commits into
Automattic:masterfrom
pgk:master

Conversation

@pgk

@pgk pgk commented Mar 28, 2017

Copy link
Copy Markdown

This is useful for local REST API development or for embedding the console as a plugin into an existing wordpress installation.

  • introduces a new auth provider
  • introduces a new api creator (core with autodiscovery)

It requires a predefined JavaScript global object is
available on the page.

The name of that global is defined in config.json under globalObjectName

example config:

{
  "wordpress.org": [
    {
      "name": "localDev",
      "authType": "local_dev",
      "globalObjectName": "wpApiSettings",
      "url": "http://no-need-for-url.com"
    }
  ]
}

The JS object can be obtained like this (in this case the
globalObjectName in the config should be named wpApiSettings)

wp_localize_script( 'wp-api', 'wpApiSettings', array(
  'nonce' => wp_create_nonce( 'wp_rest' ),
  'url' => esc_url_raw( site_url() ),
  'baseUrl' => esc_url_raw( trailingslashit( rest_url() ) ),
  'loggedInUserId' => get_current_user_id()
) );
pgk added 4 commits March 28, 2017 20:46
This provider is supposed to run on a local wp install for dev purposes.
It makes use of
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#cookie-authentication
Works by getting all namespaces of an activee wordpress install
This authorization method expects a predefined JS global object is already
available on the page it is included.

The name of the global is defined in config.json under globalObjectName

example config:

```json
{
  "wordpress.org": [
    {
      "name": "localDev",
      "authType": "local_dev",
      "globalObjectName": "wpApiSettings",
      "url": "http://no-need-for-url.com"
    }
  ]
}
```

the object itself can be abtained in PHP like this (in this case the
global in the config should be named `wpApiSettings`

```php
wp_localize_script( 'wp-api', 'wpApiSettings', array(
  'nonce' => wp_create_nonce( 'wp_rest' ),
  'url' => esc_url_raw( site_url() ),
  'baseUrl' => esc_url_raw( trailingslashit( rest_url() ) ),
  'loggedInUserId' => get_current_user_id()
) );
```
@pgk
pgk requested a review from nylen March 28, 2017 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant