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

Add basicauth support to gatsby-source-drupal #9848

Merged
merged 4 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/using-drupal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"gatsby-plugin-offline": "^2.0.5",
"gatsby-plugin-sharp": "^2.0.5",
"gatsby-plugin-typography": "^2.2.0",
"gatsby-source-drupal": "^2.2.0",
"gatsby-source-drupal": "^3.0.9",
"gatsby-transformer-sharp": "^2.1.1",
"glamor": "^2.20.40",
"gray-percentage": "^2.0.0",
Expand Down
45 changes: 37 additions & 8 deletions packages/gatsby-source-drupal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,41 @@ been used since jsonapi version `8.x-1.0-alpha4`.

```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-drupal`,
options: {
baseUrl: `https://live-contentacms.pantheonsite.io/`,
apiBase: `api`, // optional, defaults to `jsonapi`
module.exports = {
plugins: [
{
resolve: `gatsby-source-drupal`,
options: {
baseUrl: `https://live-contentacms.pantheonsite.io/`,
apiBase: `api`, // optional, defaults to `jsonapi`
},
},
},
]
],
}
```

### Basic Auth

You can use `basicAuth` option if your site is protected by basicauth.
First, you need a way to pass environment variables to the build process, so secrets and other secured data aren't committed to source control. We recommend using [`dotenv`][dotenv] which will then expose environment variables. [Read more about dotenv and using environment variables here][envvars]. Then we can _use_ these environment variables and configure our plugin.

```javascript
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-source-drupal`,
options: {
baseUrl: `https://live-contentacms.pantheonsite.io/`,
apiBase: `api`, // optional, defaults to `jsonapi`
basicAuth: {
username: process.env.BASIC_AUTH_USERNAME,
password: process.env.BASIC_AUTH_PASSWORD,
},
},
},
],
}
```

## How to query
Expand All @@ -49,3 +75,6 @@ You can query nodes created from Drupal like the following:
}
}
```

[dotenv]: https://github.com/motdotla/dotenv
[envvars]: https://www.gatsbyjs.org/docs/environment-variables
6 changes: 3 additions & 3 deletions packages/gatsby-source-drupal/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const createContentDigest = obj =>

exports.sourceNodes = async (
{ actions, getNode, hasNodeChanged, store, cache, createNodeId },
{ baseUrl, apiBase }
{ baseUrl, apiBase, basicAuth }
) => {
const { createNode } = actions

Expand All @@ -40,7 +40,7 @@ exports.sourceNodes = async (
// .lastFetched
// }

const data = await axios.get(`${baseUrl}/${apiBase}`)
const data = await axios.get(`${baseUrl}/${apiBase}`, { auth: basicAuth })
const allData = await Promise.all(
_.map(data.data.links, async (url, type) => {
if (type === `self`) return
Expand All @@ -54,7 +54,7 @@ exports.sourceNodes = async (

let d
try {
d = await axios.get(url)
d = await axios.get(url, { auth: basicAuth })
} catch (error) {
if (error.response && error.response.status == 405) {
// The endpoint doesn't support the GET method, so just skip it.
Expand Down