So, my current setup is the following: I have a Nest.js application, which takes all the routing from ForestAdmin and plugs it under /admin
path (Nest.js is using express
too). As I discovered, if I’ll create a fresh new Express instance (without Nest.js) and connect ForestAdmin routing to it, then ECONNRESET
error during client id retrieval is gone.
This is not related to ForestAdmin then, but rather some issue in how Nest.js wraps Express and I’ll keep investigating it (in the end I can just use FOREST_CLIENT_ID
env variable).
However, even with plain Express used, I’m experiencing authorization error in the later stage.
This is my main Express config (index.js):
const express = require('express');
const admin = require('../juice-app/backend/admin/app.js');
const app = express();
app.use('/admin', admin);
app.listen(8000);
And this is included app.js
, which was generated by Lumber some time ago and updated for v7 as described in the migration guide:
const express = require('express');
const requireAll = require('require-all');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const cors = require('cors');
const jwt = require('express-jwt');
const morgan = require('morgan');
const {
ensureAuthenticated,
PUBLIC_ROUTES,
} = require('forest-express-sequelize');
// also tried: const app = express.Router(); but no difference
const app = express();
app.use(morgan('tiny'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
let allowedOrigins = [/\.forestadmin\.com$/, /localhost:\d{4}$/ ];
if (process.env.CORS_ORIGINS) {
allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(','));
}
const corsConfig = {
origin: allowedOrigins,
allowedHeaders: ['Authorization', 'X-Requested-With', 'Content-Type'],
maxAge: 86400, // NOTICE: 1 day
credentials: true,
};
app.use('/forest/authentication', cors({
...corsConfig,
origin: corsConfig.origin.concat('null')
}));
app.use(cors(corsConfig));
app.use(jwt({
secret: process.env.FOREST_AUTH_SECRET,
credentialsRequired: false,
}));
app.use('/forest', (request, response, next) => {
if (PUBLIC_ROUTES.includes(request.url)) {
return next();
}
return ensureAuthenticated(request, response, next);
});
requireAll({
dirname: path.join(__dirname, 'routes'),
recursive: true,
resolve: Module => app.use('/forest', Module),
});
requireAll({
dirname: path.join(__dirname, 'middlewares'),
recursive: true,
resolve: Module => Module(app),
});
module.exports = app;
and here is the Liana initialization:
const chalk = require('chalk');
const path = require('path');
const Liana = require('forest-express-sequelize');
const { objectMapping, connections } = require('../models');
module.exports = async function (app) {
app.use(await Liana.init({
configDir: path.join(__dirname, '../forest'),
envSecret: process.env.FOREST_ENV_SECRET,
authSecret: process.env.FOREST_AUTH_SECRET,
objectMapping,
connections,
}));
console.log(chalk.cyan('Your admin panel is available here: https://app.forestadmin.com/projects'));
};
The app is started with the following command:
APPLICATION_URL=http://localhost:8000/admin DATABASE_URL=postgres://postgres@postgres:5432/juice_app DATABASE_SCHEMA=public DATABASE_SSL=false DATABASE_ENCRYPT=false FOREST_AUTH_SECRET=xxxxxxxxxxxxxxxx FOREST_ENV_SECRET=xxxxxxxxxxxxxxxxxxx node index.js
And it breaks (returns 401) when doing a call to authorization callback url ( http://localhost:8000/admin/forest/authentication/callback?code=xxxxxxxxxxxxxxxxxxxxxx&state=%7B%22renderingId%22%3A76844%7D
)
When I remove /admin path but keep wrapping ForestAdmin into external express instance everything works like it should.
So my working index.js:
const express = require('express');
const admin = require('../juice-app/backend/admin/app.js');
const app = express();
app.use('/', admin);
app.listen(8000);
Running command:
APPLICATION_URL=http://localhost:8000 DATABASE_URL=postgres://postgres@postgres:5432/juice_app DATABASE_SCHEMA=public DATABASE_SSL=false DATABASE_ENCRYPT=false FOREST_AUTH_SECRET=xxxxxxxxxxxxxxxx FOREST_ENV_SECRET=xxxxxxxxxxxxxxxxxxx node index.js
And url in environment config in ForestAdmin panel updated to http://localhost:8000
Let me know if it’s clear or shall I attach a Github repo with reproduction