Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session always undefined inside a generic middleware #64

Open
Paroca72 opened this issue Jul 11, 2023 · 3 comments
Open

Session always undefined inside a generic middleware #64

Paroca72 opened this issue Jul 11, 2023 · 3 comments

Comments

@Paroca72
Copy link

I'm trying to understand if a user is authenticated after I login using the default Admin.js login procedure.
I'm not using the admin.js router but just a simple express.js router like:

    app.use('/dashboard', [
        authMiddleware,
        dashboardRouting,
    ]);

But if I asking for Session inside the middleware is always undefined.
Where is a way to understand if I am authenticated in a generic middleware?

@dziraf
Copy link

dziraf commented Jul 11, 2023

Session store and middleware are created specifically inside AdminJS router so you cannot access the session object in outside routes, you'd have to do something like:

export const getAdminRouter = (admin: AdminJS) => {
  const router = Router({ mergeParams: true });

  const sessionOpts = {
    secret: config.session.secret,
    saveUninitialized: config.session.saveUninitialized,
    resave: config.session.resave,
    store: sessionStore,
  };

  router.use(
    session({
      ...sessionOpts,
      name: 'adminjs',
    }),
  );

  router.post('/endpoint', /* ... session available ... */);

  const modifiedRouter = ExpressPlugin.buildAuthenticatedRouter(
    admin,
    {
      cookiePassword: config.session.secret as string,
      cookieName: 'adminjs',
      authenticate,
    },
    router,
    {
      secret: config.session.secret,
      saveUninitialized: config.session.saveUninitialized,
      resave: config.session.resave,
      store: sessionStore,
    },
  );

  return modifiedRouter;
};

@Paroca72
Copy link
Author

So no way to know if I'm "Admin.js" authenticated in a generic middleware?

@dziraf
Copy link

dziraf commented Jul 11, 2023

No, only if you use session middleware with identical options in your other routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants