'Different sessions for different routes in express

I am authenticating for admin and employee roles. And using two session for each routes like this.


// session
const adminSessoin = session({
  secret: process.env.ADMIN_SECRET,
  resave: false,
  saveUninitialized: false,
  store,
  cookie: {
    maxAge: 20000,
    secure: false,
  },
});

const employeeSession = session({
  secret: process.env.EMPLOYEE_SECRET,
  resave: false,
  saveUninitialized: false,
  store,
  cookie: {
    maxAge: 20000,
    secure: false,
  },
});

app.use("/api/admin", adminSessoin, adminRoutes);
app.use("/api/employee", employeeSession, employeeRoutes);

app.get("/api", (req, res) => {
  res.send("Api is running");
});

AdminLoginController

const adminLoginController = asyncHandler(async (req, res) => {
    req.session.adminAuthenticated = true;
    req.session.admin = { pass: pass };
    res.send("success");
});

employeeLoginController

const employeeLoginController = asyncHandler(async (req, res) => {
    req.session.employeeAuthenticated = true;
    req.session.employee = { user,pass };
    res.send("success");
});

I could not figure out how to use same session for two routes. So did like this. I am here with the question if this is right way to use the session for different routes?
If i am lacking something do let me know. And for logout, I am simply req.session.destroy() destroying the session individually. Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source