Problem description
Hi, I’m currently in the process of migrating a forest-express-sequelize project to the @forestadmin/agent. I’ve replaced the forest and route folders and all their smart action logic to a new customization folder in line with the documentation recommendations. However, I’m struggling to find any documentation about how to migrate higher level authentication logic. Here is the app.js file for the forest-express-sequelize application:
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 {
errorHandler,
ensureAuthenticated,
PUBLIC_ROUTES,
} = require("forest-express-sequelize");
const app = express();
let allowedOrigins = [/\.forestadmin\.com$/, /localhost:\d{4}$/];
if (process.env.CORS_ORIGINS) {
allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(","));
}
const corsConfig = {
origin: allowedOrigins,
maxAge: 86400, // NOTICE: 1 day
credentials: true,
};
app.use(morgan("tiny"));
// Support for request-private-network as the `cors` package
// doesn't support it by default
// See: https://github.com/expressjs/cors/issues/236
app.use((req, res, next) => {
if (req.headers["access-control-request-private-network"]) {
res.setHeader("access-control-allow-private-network", "true");
}
next(null);
});
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));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(
jwt({
secret: process.env.FOREST_AUTH_SECRET,
credentialsRequired: false,
algorithms: ["HS256"],
})
);
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),
});
app.use(errorHandler());
module.exports = app;
As expected, I’ve removed the following block:
requireAll({
dirname: path.join(__dirname, "routes"),
recursive: true,
resolve: (Module) => app.use("/forest", Module),
});
Where I’m struggling, however, is finding any documentation on how to migrate the logic from
app.use("/forest", (request, response, next) => {
if (PUBLIC_ROUTES.includes(request.url)) {
return next();
}
return ensureAuthenticated(request, response, next);
});
Firstly, @forestadmin/agent doesn’t expose the public routes, so how do I check whether the requested route requires authentication or not? Secondly, @forestadmin/agent no longer provides an ensureAuthenticated, so does this need to be replaced by custom authentication logic? Lastly, @forestadmin/agent doesn’t seem to expose an errorHandler anymore, so does this again need to be replaced by custom error handling code?
If there are any other required code changes in addition to what I’ve mentioned that are recommended when migrating, it would be extremely helpful if somebody could let me know. Thank you
Context
- Project name: …
- Team name: …
- Environment name: local
- Agent technology: nodejs
- Agent (forest package) name & version: @forestadmin/agent 1.51.0
- Database type: postgreSQL
- Recent changes made on your end if any: migrating smart actions to new agent