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

Routing homepage for logged and non logged users with custom page #12271

Open
smontiu opened this issue Jan 8, 2024 · 1 comment
Open

Routing homepage for logged and non logged users with custom page #12271

smontiu opened this issue Jan 8, 2024 · 1 comment

Comments

@smontiu
Copy link

smontiu commented Jan 8, 2024

Description

Hi!

I use a custom page plugin to have a landing page for my forum, but the configuration only allows to have one custom page for all kinds of users. I will like to have the option to set a different custom page for logged users, and no logged users. Root them to "categories" instead of the custom page (landing page).

I am pretty sure that more users would like to do the same thing.

Community forum reference

No response

@barisusakli
Copy link
Member

barisusakli commented Jan 9, 2024

You should be able to do this using the hook action:homepage.get:<pathname> https://github.com/NodeBB/NodeBB/blob/master/src/controllers/home.js#L55

Assuming your page is called custom the name of the hook that would fire would be action:homepage.get:custom. You add a entry in your plugin.json hooks array like so:

{ "hook": "action:homepage.get:custom", "method": "actionHomePageGetCustom" }

And implement it in your plugin library.js file like below.

library.actionHomePageGetCustom = async ({ req, res, next }) => {
	// redirect non-logged in users to categories
	if (req.uid <= 0) {
		const controllersHelpers = require.main.require('./src/controllers/helpers');
		controllersHelpers.redirect(res, '/categories');
		return;
	}
	// default action, redirect to custom page
	const pathname = res.locals.homePageRoute;
	req.url = req.path + (!req.path.endsWith('/') ? '/' : '') + pathname;
	next();
};

This would cause logged in users to see the custom page and everyone else would get redirected to /categories

Let me know if that helps.

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

No branches or pull requests

2 participants