Skip to content

Commit

Permalink
Revert "Merge branch 'gatsby-oauth' into dev"
Browse files Browse the repository at this point in the history
This reverts commit 29addae, reversing
changes made to 9e49fb7.
  • Loading branch information
colorfield committed May 2, 2024
1 parent 36d9969 commit af1d7d4
Show file tree
Hide file tree
Showing 31 changed files with 80 additions and 1,143 deletions.
19 changes: 4 additions & 15 deletions INIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,17 @@ replace(
'PROJECT_NAME=example',
'PROJECT_NAME=' + process.env.PROJECT_NAME_MACHINE,
);
const publisherClientSecret = randomString(32);
const clientSecret = randomString(32);
replace(
['apps/cms/.lagoon.env', 'apps/website/.lagoon.env'],
'PUBLISHER_OAUTH2_CLIENT_SECRET=REPLACE_ME',
`PUBLISHER_OAUTH2_CLIENT_SECRET=${publisherClientSecret}`,
'PUBLISHER_OAUTH2_CLIENT_SECRET=' + clientSecret,
);
const publisherSessionSecret = randomString(32);
const sessionSecret = randomString(32);
replace(
['apps/website/.lagoon.env'],
'PUBLISHER_OAUTH2_SESSION_SECRET=REPLACE_ME',
`PUBLISHER_OAUTH2_SESSION_SECRET=${publisherSessionSecret}`,
);
const websiteClientSecret = randomString(32);
replace(
['apps/cms/.lagoon.env'],
'WEBSITE_OAUTH2_CLIENT_SECRET=REPLACE_ME',
`WEBSITE_OAUTH2_CLIENT_SECRET=${websiteClientSecret}`,
);
console.log(
'Website OAuth2 environment variables to be set in Netlify',
`AUTH_DRUPAL_ID: website`,
`AUTH_DRUPAL_SECRET: ${websiteClientSecret}`,
'PUBLISHER_OAUTH2_SESSION_SECRET=' + sessionSecret,
);
// Template's prod domain is special.
replace(
Expand Down
59 changes: 0 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ Other steps

- [Create a new Lagoon project](https://amazeelabs.atlassian.net/wiki/spaces/ALU/pages/368115717/Create+a+new+Lagoon+project)
- [Create a new Netlify project](https://amazeelabs.atlassian.net/wiki/spaces/ALU/pages/368017428/Create+a+new+Netlify+project)
- Set `AUTH_DRUPAL_ID` and `AUTH_DRUPAL_SECRET` in
[Netlify environment variables](#gatsby-authentication--sso)
- Check the [Environment overrides](#environment-overrides) section below
- Check the [Choose a CMS](#choose-a-cms) section below
- Create `dev` and `prod` branches (and optionally `stage`) from `release`
Expand Down Expand Up @@ -219,63 +217,6 @@ lagoon runtime configuration.
lagoon add variable -p [project name] -e dev -N NETLIFY_SITE_ID -V [netlify site id]
```

### Gatsby authentication / SSO

Authentication providers are relying on Auth.js (formerly Next-Auth) and can be
configured in `/apps/website/nextauth.config.js`

An example provider is available for Drupal.

On Netlify, several environment variables are required to be set:

#### For all providers

- `NEXTAUTH_URL` The URL of the frontend. This is used for the callback.
- `NEXTAUTH_SECRET` A random string used for encryption.

Generate the secret with e.g. `openssl rand -base64 32`

#### For Drupal

- `AUTH_DRUPAL_ID` The client ID of the Drupal Consumer
- `AUTH_DRUPAL_SECRET` The client secret of the Drupal Consumer

Drupal environment variables are displayed in the console when running
`pnpx @amazeelabs/mzx run INIT.md`.

<details>
<summary>How it works</summary>
A `Website` consumer is created in Drupal `/admin/config/services/consumer` with

- Label: `Website`
- Client ID: `website`
- Secret: a random string matching `AUTH_DRUPAL_SECRET`
- Redirect URI: `[netlify-gatsby-site-url]/api/auth/callback/drupal`

#### Other providers

Refer to [Auth.js documentation](https://next-auth.js.org/providers/).

</details>

<details>
<summary>Local development</summary>

#### Start Drupal and Gatsby

- Drupal: in `/apps/cms` - `pnpm start` use http://127.0.0.1:8888
- Gatsby: in `/apps/website` - `pnpm gatsby:develop` use
http://localhost:8000/en

#### Basic troubleshooting

- Make sure to have keys generated
http://127.0.0.1:8888/en/admin/config/people/simple_oauth
- Make sure to have the correct client id and secret set
http://127.0.0.1:8888/en/admin/config/services/consumer/2/edit

</details>

### Publisher authentication with Drupal

Publisher can require to authenticate with Drupal based on OAuth2. It is only
Expand Down
5 changes: 1 addition & 4 deletions apps/cms/.lagoon.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ PUBLISHER_URL="https://build.${LAGOON_ENVIRONMENT}.${LAGOON_PROJECT}.ch4.amazee.
NETLIFY_URL="https://build.${LAGOON_ENVIRONMENT}.${LAGOON_PROJECT}.ch4.amazee.io"
PREVIEW_URL="https://preview.${LAGOON_ENVIRONMENT}.${LAGOON_PROJECT}.ch4.amazee.io"

# Used to set the original client secret for Publisher.
# Used to set the original client secret.
PUBLISHER_OAUTH2_CLIENT_SECRET=REPLACE_ME

# Used to set the original client secret for the Website.
WEBSITE_OAUTH2_CLIENT_SECRET=REPLACE_ME
4 changes: 0 additions & 4 deletions apps/website/gatsby-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import '@custom/ui/styles.css';

import { GatsbyBrowser } from 'gatsby';

import { WrapRootElement } from './src/utils/wrapRootElement';

export const wrapRootElement = WrapRootElement;

export const shouldUpdateScroll: GatsbyBrowser['shouldUpdateScroll'] = (
args,
) => {
Expand Down
2 changes: 2 additions & 0 deletions apps/website/gatsby-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// TS file name should be different from gastby-config.ts, otherwise Gatsby will
// pick it up instead of the JS file.

import { existsSync } from 'fs';

process.env.NETLIFY_URL = process.env.NETLIFY_URL || 'http://127.0.0.1:8000';

process.env.CLOUDINARY_API_KEY = process.env.CLOUDINARY_API_KEY || 'test';
Expand Down
8 changes: 0 additions & 8 deletions apps/website/gatsby-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ export const createPages = async ({ actions }) => {
});
});

// Create a profile page in each language.
Object.values(Locale).forEach((locale) => {
actions.createPage({
path: `/${locale}/profile`,
component: resolve(`./src/templates/profile.tsx`),
});
});

// Broken Gatsby links will attempt to load page-data.json files, which don't exist
// and also should not be piped into the strangler function. Thats why they
// are caught right here.
Expand Down
57 changes: 0 additions & 57 deletions apps/website/nextauth.config.js

This file was deleted.

9 changes: 1 addition & 8 deletions apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
"@custom/decap": "workspace:*",
"@custom/schema": "workspace:*",
"@custom/ui": "workspace:*",
"@gatsbyjs/reach-router": "^2.0.1",
"@netlify/plugin-nextjs": "^5.1.2",
"babel-loader": "^9.1.3",
"body-parser": "^1.20.2",
"gatsby": "^5.13.1",
"gatsby-plugin-layout": "^4.13.0",
"gatsby-plugin-manifest": "^5.13.0",
Expand All @@ -31,10 +27,7 @@
"gatsby-source-filesystem": "^5.13.0",
"image-size": "^1.1.1",
"mime-types": "^2.1.35",
"multer": "1.4.5-lts.1",
"netlify-cli": "^17.21.1",
"next": "^14.2.3",
"next-auth": "^4.24.7",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand All @@ -60,7 +53,7 @@
"serve": "netlify dev --cwd=. --dir=public --port=8000",
"dev": "pnpm clean && publisher",
"open": "open http://127.0.0.1:8000/___status/",
"gatsby:develop": "NEXTAUTH_URL=http://localhost:8000 NEXTAUTH_SECRET=banana ENABLE_GATSBY_REFRESH_ENDPOINT=true pnpm gatsby develop",
"gatsby:develop": "ENABLE_GATSBY_REFRESH_ENDPOINT=true pnpm gatsby develop",
"gatsby:refresh": "curl -X POST http://localhost:8000/__refresh",
"clean": "gatsby clean"
}
Expand Down
10 changes: 0 additions & 10 deletions apps/website/src/api/auth/[...nextauth].js

This file was deleted.

2 changes: 1 addition & 1 deletion apps/website/src/templates/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function Head({ data }: HeadProps<typeof query>) {
export default function PageTemplate({ data }: PageProps<typeof query>) {
// Retrieve the current location and prefill the
// "ViewPageQuery" with these arguments.
// That makes sure the `useOperation(ViewPageQuery, ...)` with this
// That makes shure the `useOperation(ViewPageQuery, ...)` with this
// path immediately returns this data.
const [location] = useLocation();
return (
Expand Down
38 changes: 0 additions & 38 deletions apps/website/src/templates/profile.tsx

This file was deleted.

31 changes: 1 addition & 30 deletions apps/website/src/utils/drupal-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,10 @@ export function drupalExecutor(endpoint: string, forward: boolean = true) {
return async function <OperationId extends AnyOperationId>(
id: OperationId,
variables?: OperationVariables<OperationId>,
accessToken?: string,
) {
const url = new URL(endpoint, window.location.origin);
const isMutation = id.includes('Mutation:');
const isAuthenticated = accessToken !== undefined;

if (isAuthenticated) {
const { data, errors } = await (
await fetch(url, {
method: 'POST',
body: JSON.stringify({
queryId: id,
variables: variables,
}),
headers: forward
? {
'SLB-Forwarded-Proto': window.location.protocol.slice(0, -1),
'SLB-Forwarded-Host': window.location.hostname,
'SLB-Forwarded-Port': window.location.port,
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
}
: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
})
).json();
if (errors) {
throw errors;
}
return data;
} else if (isMutation) {
if (isMutation) {
const { data, errors } = await (
await fetch(url, {
method: 'POST',
Expand Down
7 changes: 0 additions & 7 deletions apps/website/src/utils/wrapRootElement.tsx

This file was deleted.

Loading

0 comments on commit af1d7d4

Please sign in to comment.