I was able to get this working in Heroku for Review App PRs (and didn’t have time to convert into a generic Heroku addon, so sharing Heroku version inspired by Netlify solution). Just change the environment variables (this example also assumes create-react-app based application):
package.json
{
...
"scripts": {
"review-app-postdeploy": "node scripts/review-app-postdeploy.js",
"review-app-pr-predestroy": "node scripts/review-app-pr-predestroy.js",
},
...
}
app.json (for Heroku)
{
"environments": {
"review": {
"scripts": {
"postdeploy": "yarn review-app-postdeploy",
"pr-predestroy": "yarn review-app-pr-predestroy"
}
}
},
}
scripts/review-app-postdeploy.js
const auth0 = require('auth0');
const dotenv = require('dotenv');
dotenv.config();
console.log(`🔑 Auth0 Plugin startup`);
const tab = ' ';
const requiredEnvVariables = [
'REACT_APP_AUTH0_DOMAIN',
'REACT_APP_AUTH0_CLIENT_ID',
'AUTH0_MANAGEMENT_CLIENT_ID',
'AUTH0_MANAGEMENT_CLIENT_SECRET'
];
const missingEnvVariables = requiredEnvVariables.filter(
(envVar) => typeof process.env[envVar] === 'undefined'
);
if (missingEnvVariables.length > 0) {
console.log(
`${tab} ☠️ Missing environment variables: ${missingEnvVariables.join(
', '
)}`
);
}
const deployUrl = `https://${process.env.HEROKU_APP_NAME}.herokuapp.com`;
console.log(`${tab} 🧭 Deploy Preview URL will be:`, deployUrl);
const management = new auth0.ManagementClient({
domain: process.env.REACT_APP_AUTH0_DOMAIN,
clientId: process.env.AUTH0_MANAGEMENT_CLIENT_ID,
clientSecret: process.env.AUTH0_MANAGEMENT_CLIENT_SECRET,
scope: 'read:clients update:clients',
audience: 'https://XXXXXX.us.auth0.com/api/v2/'
});
management.clients
.get({ client_id: process.env.REACT_APP_AUTH0_CLIENT_ID })
.then((client) => {
console.log(`${tab} 🗝 Retrieved Auth0 client: ${client.name}`);
const clientWebOrigins = client.web_origins || [];
const urlOrigins = [deployUrl];
const urlsToAdd = urlOrigins.filter(
(url) => !clientWebOrigins.includes(url)
);
if (urlsToAdd.length > 0) {
console.log(
`${tab} Adding URLs to the Auth0 Application Web Origins:`
);
urlsToAdd.forEach((url) => console.log(`${tab} • ${url}`));
management.clients.update(
{ client_id: process.env.REACT_APP_AUTH0_CLIENT_ID },
{ web_origins: clientWebOrigins.concat(urlsToAdd) },
(updateError) => {
if (updateError) {
utils.build.failPlugin(
`${tab} ☠️ Something wrong happened while trying to patch Auth0 Application`
);
} else {
console.log(
`${tab} 🍾 Successfully patched Auth0 Application.`
);
}
}
);
} else {
console.log(
`${tab} 👍 URL has already been added to Auth0 Application`
);
}
});
And, scripts/review-app-pr-predestroy.js
const auth0 = require('auth0');
const dotenv = require('dotenv');
dotenv.config();
console.log(`🔑 Auth0 Plugin startup`);
const tab = ' ';
const requiredEnvVariables = [
'REACT_APP_AUTH0_DOMAIN',
'REACT_APP_AUTH0_CLIENT_ID',
'AUTH0_MANAGEMENT_CLIENT_ID',
'AUTH0_MANAGEMENT_CLIENT_SECRET'
];
const missingEnvVariables = requiredEnvVariables.filter(
(envVar) => typeof process.env[envVar] === 'undefined'
);
if (missingEnvVariables.length > 0) {
console.log(
`${tab} ☠️ Missing environment variables: ${missingEnvVariables.join(
', '
)}`
);
}
const deployUrl = `https://${process.env.HEROKU_APP_NAME}.herokuapp.com`;
console.log(`${tab} 🧭 Deploy Preview URL will be:`, deployUrl);
const management = new auth0.ManagementClient({
domain: process.env.REACT_APP_AUTH0_DOMAIN,
clientId: process.env.AUTH0_MANAGEMENT_CLIENT_ID,
clientSecret: process.env.AUTH0_MANAGEMENT_CLIENT_SECRET,
scope: 'read:clients update:clients',
audience: 'https://XXXXXX.us.auth0.com/api/v2/'
});
management.clients
.get({ client_id: process.env.REACT_APP_AUTH0_CLIENT_ID })
.then((client) => {
console.log(`${tab} 🗝 Retrieved Auth0 client: ${client.name}`);
const clientWebOrigins = client.web_origins || [];
const urlsToRemove = [deployUrl];
if (urlsToRemove.length > 0) {
console.log(
`${tab} Removing URLs to the Auth0 Application Web Origins:`
);
urlsToRemove.forEach((url) => console.log(`${tab} • ${url}`));
management.clients.update(
{ client_id: process.env.REACT_APP_AUTH0_CLIENT_ID },
{
web_origins: clientWebOrigins.filter(
(origin) => !urlsToRemove.includes(origin)
)
},
(updateError) => {
if (updateError) {
utils.build.failPlugin(
`${tab} ☠️ Something wrong happened while trying to patch Auth0 Application`
);
} else {
console.log(
`${tab} 🍾 Successfully patched Auth0 Application.`
);
}
}
);
} else {
console.log(
`${tab} 👍 URL has already been removed to Auth0 Application`
);
}
});