TriliumNext / Notes

Build your personal knowledge base with TriliumNext Notes
https://triliumnext.github.io/Docs/
GNU Affero General Public License v3.0
1.06k stars 60 forks source link

Redirect anonymous bare url to /share page #658

Open maphew opened 2 days ago

maphew commented 2 days ago

Discussed in https://github.com/orgs/TriliumNext/discussions/596

Originally posted by **maphew** November 15, 2024 How to setup Trilium to redirect bare url to the share page, unless the user is logged in? So an anonymous browser arriving at `www.example.net` is redirected to `www.example.net/share` instead of being greeted with the login page. Or perhaps better, make the share url `www.example.net/` and the "I'm editing" url `www.example.net/edit` ? preferably using a docker config (so it can be used on PikaPods)

At the technical level, it's actually easy to redirect bare domain to share:

  1. In trilium create or choose a note to be your share landing page and set an owned attribute of #shareRoot.

image

  1. In ./src/services/auth.ts change line 20 from res.redirect("login") to res.redirect("share").
function checkAuth(req: AppRequest, res: Response, next: NextFunction) {
    if (!sqlInit.isDbInitialized()) {
        res.redirect("setup");
    }
    else if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
        #res.redirect("login");
        res.redirect("share");
    }
    else {
        next();
    }
}
  1. Restart Trilium server

To login, edit url in address bar and replace path with '/login', ex: https://my.trilium.net/login

...

This won't work for PikaPods since they only use the lates release stable release from Docker Hub, and making this change in stable needs more thought, work, and documentation.

maphew commented 2 days ago

As written above, this would be a major change in out of the box Trilium behaviour, so not to be approached lightly. With my recent learning success in adding a whitelist of html tags to the Options page I think I see a path to how to make this an optional setting too. We'll see how well that goes!

I don't know if this is a good approach architecturally speaking, and welcome comments from experienced minds.

perfectra1n commented 2 days ago

Would this change require a user to have a root Share note already set up?:

{
  "message": "Share root note not found"
}

I understand the reason why, but IMO since this is firstly a note-taking platform (and not a microblog), I'm not sure if this is the best idea. I understand that your suggestion is what platforms like Wordpress do where the root URL redirects to the website, and /wp-admin is reserved for management/editing. I'm sure @meichthys will have a much more sensible response.

You could do exactly what you're suggesting with access to the webserver and changing its configuration, but since you're on Pikapods I don't believe you have access to that.

maphew commented 1 day ago

Yes shareRoot needs to be defined, thanks for the reminder! I've added that note to the helper text in the PR

meichthys commented 1 day ago

First off, I think this is a neat idea, and I can see it's use case, however if it were to be implemented, it would certainly need to be a non-default option.

I would also suspect that there could be some unexpected side effects of a change like this (Elian would have a better understanding on that).

maphew commented 20 hours ago

Thanks for the feedback @meichthys. Yes it's off by default.