2026-10-04 16:36:09 +00:00
|
|
|
import { createAssetModule } from "@metis/assets/module";
|
2026-10-03 10:36:03 +00:00
|
|
|
import { createCompanyModule } from "@metis/companies/module";
|
|
|
|
|
import { createIdentityModule, migrateIdentity } from "@metis/identity/module";
|
2026-10-05 11:13:51 +00:00
|
|
|
import { createResourceModule } from "@metis/resources/module";
|
2026-10-03 10:36:03 +00:00
|
|
|
import connectSequelize from "connect-session-sequelize";
|
|
|
|
|
import session from "express-session";
|
|
|
|
|
import type { Sequelize } from "sequelize";
|
|
|
|
|
|
|
|
|
|
import type { readDatabaseEnvironment } from "../../config/database";
|
|
|
|
|
|
|
|
|
|
export async function createIdentityRuntime(
|
|
|
|
|
database: Sequelize,
|
2026-10-05 15:01:32 +00:00
|
|
|
environment: ReturnType<typeof readDatabaseEnvironment>,
|
|
|
|
|
options: { legacyResourceFixtures?: boolean } = {}
|
2026-10-03 10:36:03 +00:00
|
|
|
) {
|
|
|
|
|
await database.authenticate();
|
|
|
|
|
await migrateIdentity(database);
|
|
|
|
|
const identity = await createIdentityModule(database, environment.APP_ORIGIN);
|
2026-10-05 15:01:32 +00:00
|
|
|
const companies = await createCompanyModule(
|
|
|
|
|
database,
|
|
|
|
|
identity.authenticate,
|
|
|
|
|
options.legacyResourceFixtures === true && environment.NODE_ENV === "test"
|
|
|
|
|
);
|
2026-10-04 16:36:09 +00:00
|
|
|
const assets = await createAssetModule(database, identity.authenticate);
|
2026-10-05 11:13:51 +00:00
|
|
|
const resources = await createResourceModule(database, identity.authenticate);
|
2026-10-04 21:06:07 +00:00
|
|
|
const mail = await companies.initializeMail(assets.services.inventory);
|
2026-10-05 07:53:29 +00:00
|
|
|
const profiles = await companies.initializeProfiles(assets.services.inventory);
|
2026-10-05 15:01:32 +00:00
|
|
|
await resources.migrateLegacy();
|
2026-10-03 10:36:03 +00:00
|
|
|
const SequelizeStore = connectSequelize(session.Store);
|
|
|
|
|
const store = new SequelizeStore({
|
|
|
|
|
db: database,
|
|
|
|
|
tableName: "http_sessions",
|
|
|
|
|
expiration: 8 * 60 * 60 * 1000,
|
|
|
|
|
});
|
|
|
|
|
const middleware = session({
|
|
|
|
|
name: "metis.sid",
|
|
|
|
|
secret: environment.SESSION_SECRET,
|
|
|
|
|
store,
|
|
|
|
|
resave: false,
|
|
|
|
|
saveUninitialized: false,
|
|
|
|
|
cookie: {
|
|
|
|
|
httpOnly: true,
|
|
|
|
|
sameSite: "strict",
|
|
|
|
|
secure: environment.NODE_ENV === "production",
|
|
|
|
|
maxAge: 8 * 60 * 60 * 1000,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return {
|
|
|
|
|
register: (app: Parameters<typeof identity.register>[0]) => {
|
|
|
|
|
identity.register(app);
|
|
|
|
|
companies.register(app);
|
2026-10-04 16:36:09 +00:00
|
|
|
assets.register(app);
|
2026-10-05 11:13:51 +00:00
|
|
|
resources.register(app);
|
2026-10-04 21:06:07 +00:00
|
|
|
mail.register(app);
|
2026-10-05 07:53:29 +00:00
|
|
|
profiles.register(app);
|
2026-10-03 10:36:03 +00:00
|
|
|
},
|
|
|
|
|
middleware,
|
|
|
|
|
stop: () => store.stopExpiringSessions(),
|
|
|
|
|
};
|
|
|
|
|
}
|