How can I redirect to login page if the user does not login

Hi, I’m new to SS. I know you need to login to access cms, but how can I apply this to the whole website. Like when a guest visit a url, this will be redirected to the login page and come back after login.

Actually, I’m reading others source code, but I really couldn’t find how this is achieved.

Thanks.

This is a standard part of the CMS. If you look at the ‘Settings’ tab on a page, you can choose who is allowed to view the page:

If you set this to ‘Logged in users’ then the system will automatically redirect any non-logged-in users to the login page, and then return them afterwards. By default, sub-pages will inherit these permissions from their parent.

If you need something a bit more, you can also do this in code by making a couple of changes in the default Page Controller class (see tha docs page here: https://docs.silverstripe.org/en/5/developer_guides/controllers/access_control/#controller-level-checks)

Thank You! Everything becomes easy now! :slight_smile:

I should have mentioned, you can also set this globally for the site, (which sounds like what you want to do) from the ‘Settings > Access’ menu:

That way, all the pages which have ‘Inherit from the site access settings’ will just pick up on the global value.