Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using the console with cookie authentication #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

pgk
Copy link

@pgk pgk commented Mar 28, 2017

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant