Project creation from existing Express / Sequelize NodeJS app

Hello, I’m trying to install forest admin into an existing Express / Sequelize app and some things are unclear:

During the installation process, the wizard tells me to add the following to the top of my app.js file:

const forest = require('forest-express-sequelize');
const models = require('./path/to/your/sequelize/models');
const connection = require('./path/to/your/sequelize/connection');

// Insert this middleware in your express listen callback:
app.use(await forest.init({
 envSecret: process.env.FOREST_ENV_SECRET,
 authSecret: process.env.FOREST_AUTH_SECRET,
 objectMapping: models,
 connections: { default: connection },
}));

But it is unclear to me what the models and connection variables should be, could you please provide more information ? Maybe if there is a demo app somewhere on github I could use that as a reference.

I am also getting the following warning when running the app:

[forest] 🌳🌳🌳 Your configDir ("/Users/camillefeghali/dev/foxxbee/fa-demo-mac-sequelize/forest") does not exist. Please make sure it is set correctly.

Which lends me to believe that I should create a /forest directory in my project, but nowhere in the installation steps is that mentioned.

Thanks for your time.

Hello @cooki,

With a forest-express-sequelize integration:

  • the models is expected to be the Sequelize module require result.
  • the connection is expected to be the Sequelize instance, created with your database parameters

An exemple of a models/index.js file could be something like:

const Sequelize = require("sequelize");
const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASSWORD, {
  host: DB_HOST,
  port: DB_PORT,
  dialect: DB_DIALECT,
  ...
});

const db = {};

db.models = Sequelize; // Capital "s"
db.connection = sequelize; // Lower case

db.items = require("./items.js")(sequelize, Sequelize);

module.exports = db;

The documentation snippet shows two require statements in case some project define things separately.
With the code above, you can do:

const forest = require('forest-express-sequelize');

const { connection, models } = require('./models');

app.listen(PORT, async () => {
  console.log(`Server is running on port ${PORT}.`);

  app.use(await forest.init({
   envSecret: process.env.FOREST_ENV_SECRET,
   authSecret: process.env.FOREST_AUTH_SECRET,
   objectMapping: models,
   connections: { default: connection },
  }));
});

Let me know if this is enough to solve you issue.

Hey @anon79585656, thanks for the quick reply.
That worked, thank you. I am now facing another issue. When trying to access my admin panel, I get the following screen:

After checking the network tab in chrome dev tools, I see two requests that failed:

How can we move forward from here ?

@cooki,

In the onboarding snippets, there should have been a line like:
app.use('^(?!forest/?$).*', cors(corsOptions));

This will allow our authentication requests to use their own CORS configuration.

Did you add it to your project?

In the onboarding it states that if cors is enabled I should add the snippet.

I tried many options:
1 - Not adding the app.use('^(?!forest/?$).*', cors(corsOptions)); because I don’t have cors enabled.

2 - Enabling cors and adding app.use('^(?!forest/?$).*', cors()); => Note that I do not pass a corsOptions variable, as it is not described in the wizard what that var should hold

3 - Adding the following code (taken from a standalone PostgreSQL installation app) installed using lumber:

let allowedOrigins = [
  /\.forestadmin\.com$/,
  /localhost:\d{4}$/,
  "^(?!forest/?$).*",
];

const corsConfig = {
  origin: allowedOrigins,
  maxAge: 86400, // NOTICE: 1 day
  credentials: true,
};

app.use(
  "/forest/authentication",
  cors({
    ...corsConfig,
    // The null origin is sent by browsers for redirected AJAX calls
    // we need to support this in authentication routes because OIDC
    // redirects to the callback route
    origin: corsConfig.origin.concat("null"),
  })
);
app.use(cors(corsConfig));

Same error in every scenario.

Hello @cooki,

Can you share more information (detailed screenshot) of the network calls that fails ?

Thanks in advance,
Morgan

Hey @morganperre sure,

Thanks @cooki.

It seems that the last authentication step is failing. Can you confirm that the /forest/authentication is OK ? And only the /forest/authentication/callback fails ?

I have more questions to find the culprit.

  • On which PORT do you run your application ?
  • Did you define the environment variables during the onboarding process (FOREST_ENV_SECRET, FOREST_AUTH_SECRET, APPLICATION_URL) ?

Thanks,
Morgan

@morganperre I hadn’t added the APPLICATION_URL in my .env because I couldn’t see where it was used. My panel loads now that I added it.

Could you explain to me where that env var is used ?

Thanks a lot for your help.

2 Likes

The APPLICATION_URL is used to create the callback URL. If you specified a specific url for your application in place of the default one (for example for an install on a remote machine), this url should be set to the right URL to reach your app.

If think we miss some documentation during the onboarding. I’ll talk to my teammate to review this step. Thanks for the head up.

Have a nice day.
Morgan

2 Likes