How to change homepage after login before first page loads

I have a landing page theme component at https://ejopa.discourse.group/

It works great. I need for it not to show for logged in users. I have this code, that sets the home page in the initializer:

export default apiInitializer((api) => {
  const siteSettings = api.container.lookup("service:site-settings");
  const topMenu = siteSettings.top_menu || ""; // protect against undefined
  const globalDefaultHomepage = topMenu.split("|")[0].trim();
  const currentUser = api.getCurrentUser();

  if (currentUser && defaultHomepage() === "custom") {
    setDefaultHomepage(globalDefaultHomepage);
  }
  api.renderInOutlet("custom-homepage", LandingPage);

});

It works just great, EXCEPT for the first time they log in. They get redirected to /custom – if they click the icon, they go the right place, but not on first load..

I need to do . . . something . . . to redirect them to the right homepage when they log.

AI suggested something like this, which doesn’t seem to work

      api.onAppEvent('application:after-login', afterLoginHandler);

EDIT! Almost there, but flashes the landing page after login

import { apiInitializer } from "discourse/lib/api";
import { defaultHomepage, setDefaultHomepage } from "discourse/lib/utilities";
import LandingPage from "../components/landing-page";

export default apiInitializer((api) => {
  const siteSettings = api.container.lookup("service:site-settings");
  const topMenu = siteSettings.top_menu || ""; // protect against undefined
  const globalDefaultHomepage = topMenu.split("|")[0].trim();
  const currentUser = api.getCurrentUser();

  api.onPageChange((url) => {
    const isCustom = url === "/custom";
    const router = api.container.lookup("service:router");
    const currentUser = api.container.lookup("service:current-user");
    if (currentUser && isCustom) {
      router.transitionTo("discovery." + defaultHomepage());
    }
  });

  if (currentUser && defaultHomepage() === "custom") {
    setDefaultHomepage(globalDefaultHomepage);
  }
  api.renderInOutlet("custom-homepage", LandingPage);
});

I’m afraid this is the best I can do. Am I wrong?

I wonder if this could be due to the redirect_users_to_top_page setting, because that’s something that also redirects the user on the first visit. But since you end at /custom and not at /top, this is probably not the case.

I tried to find that but couldn’t remember what it was called!

Right.

And also “Automatically redirect new and long absent users”, and we are neither new nor long absent. . .

Thanks very much!

1 Like