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?