<p>There are several approaches to building this feature. It really depends a lot on your more concrete requirements and constraints.</p>
<h2>Basic solution with user accounts</h2>
<p>You can get this behaviour with almost no custom code by using the built-in user account system. In this scenario, all your clients (and the admin as well) will get their own user accounts to the CMS and log in with their username and password. To get started, include a <a href="https://craftcms.com/knowledge-base/front-end-user-accounts#login-form" rel="nofollow noreferrer">frontend login form</a> on the homepage - see the linked knowledge base article for details.</p>
<p>Validation, authentication and error handling are all taken care of by either the controller method used by the form or by the form template.</p>
<p>After a successful login, you can control where the user is redirected using the <code>loginPath</code> setting. You can use this in two ways:</p>
<ul>
<li>Set it to a static route like <code>/my-content</code> and just show contents for the logged-in user there.</li>
<li>You can set the path to a function that gets called every time the login path is needed. In this function, you can write custom logic to determine the right place to redirect the current user to. For example, by finding an entry that is associated with that user account and redirecting to that.</li>
</ul>
<h3>Advanced solution with a custom controller</h3>
<p>If you can't or don't want to use regular user accounts, you can also built your own endpoint for validating passwords and redirecting to the appropriate place. First, create a <a href="https://craftcms.com/docs/4.x/extend/module-guide.html" rel="nofollow noreferrer">module</a> to hold your custom logic. Inside that module, create a <a href="https://craftcms.com/docs/4.x/extend/controllers.html" rel="nofollow noreferrer">controller</a> with a controller action for processing password inputs. The action should do the following things:</p>
<ul>
<li>Check if the password belongs to a valid data set (for example, an entry with a custom <code>password</code> field that matches the given password).</li>
<li>Save the information that the current visitor is allowed to access that data set in a way that can't be manipulated from the frontend (for example, in the session).</li>
<li><a href="https://craftcms.com/docs/4.x/extend/controllers.html#redirecting-the-request" rel="nofollow noreferrer">Redirect the user</a> to that data set.</li>
</ul>
<p>Then you only need a function in your template for datasets to check if the current visitor is allowed to view that dataset. You can make that function available through Twig with a <a href="https://craftcms.com/docs/4.x/extend/extending-twig.html#register-a-twig-extension" rel="nofollow noreferrer">custom Twig extension</a> in your module. If the visitor doesn't have permission, <a href="https://craftcms.com/docs/4.x/dev/tags.html#exit" rel="nofollow noreferrer">exit</a> with a 404 or 401 status.</p>
<hr />
<p>You can mix and match those approaches to get the perfect solution to your problem.</p>

CC BY-SA 4.0
2022-05-17T18:18:22.530Z
2022-05-17T18:18:22.530Z
MoritzLost
<p>I don't know if there even is a way to do this using Craft CMS:</p>
<p>I am looking for some sort of a password protected redirect. <code>domain.com</code> should show a password mask that accepts multiple different passwords, each valid password will redirect the user to another entry. These entries should otherwise not be accessible, even when using direct links. If a visitor accesses a direct link to a entry, it should redirect him to the login mask.</p>
<p><strong>Example:</strong> If the password <code>admin</code> is typed in, a user would be redirected to a specific entry <code>domain.com/admin</code>, whilst the password <code>clientname</code> may lead to another specific entry <code>domain.com/client</code>.</p>
<p><strong>Use case in detail:</strong> Planned is a file/entry sharing hub. A client should be able to access <code>domain.com</code> and after &quot;logging in&quot; using his individual access code see all the entries assigned to him. Those entries can only be accessed after &quot;logging in&quot;.</p>
<p>Maybe I am thinking waaay to far and should just be doing a simple User System using Craft Pro?</p>

<redirect><craft-pro><password>
Redirect to entry based on password
# Redirect to entry based on password

(Asked by: username_0 on 2022-05-14)

I don't know if there even is a way to do this using Craft CMS:

I am looking for some sort of a password protected redirect. `domain.com` should show a password mask that accepts multiple different passwords, each valid password will redirect the user to another entry. These entries should otherwise not be accessible, even when using direct links. If a visitor accesses a direct link to a entry, it should redirect him to the login mask.

**Example:** If the password `admin` is typed in, a user would be redirected to a specific entry `domain.com/admin`, whilst the password `clientname` may lead to another specific entry `domain.com/client`.

**Use case in detail:** Planned is a file/entry sharing hub. A client should be able to access `domain.com` and after "logging in" using his individual access code see all the entries assigned to him. Those entries can only be accessed after "logging in".

Maybe I am thinking waaay to far and should just be doing a simple User System using Craft Pro?

**Tags:** redirect, craft-pro, password


---

## 1 answers

### Answer by username_1 on 2022-05-17. Score: 1

There are several approaches to building this feature. It really depends a lot on your more concrete requirements and constraints.

## Basic solution with user accounts

You can get this behaviour with almost no custom code by using the built-in user account system. In this scenario, all your clients (and the admin as well) will get their own user accounts to the CMS and log in with their username and password. To get started, include a [frontend login form](https://craftcms.com/knowledge-base/front-end-user-accounts#login-form) on the homepage - see the linked knowledge base article for details.

Validation, authentication and error handling are all taken care of by either the controller method used by the form or by the form template.

After a successful login, you can control where the user is redirected using the `loginPath` setting. You can use this in two ways:

* Set it to a static route like `/my-content` and just show contents for the logged-in user there.
* You can set the path to a function that gets called every time the login path is needed. In this function, you can write custom logic to determine the right place to redirect the current user to. For example, by finding an entry that is associated with that user account and redirecting to that.

### Advanced solution with a custom controller

If you can't or don't want to use regular user accounts, you can also built your own endpoint for validating passwords and redirecting to the appropriate place. First, create a [module](https://craftcms.com/docs/4.x/extend/module-guide.html) to hold your custom logic. Inside that module, create a [controller](https://craftcms.com/docs/4.x/extend/controllers.html) with a controller action for processing password inputs. The action should do the following things:

* Check if the password belongs to a valid data set (for example, an entry with a custom `password` field that matches the given password).
* Save the information that the current visitor is allowed to access that data set in a way that can't be manipulated from the frontend (for example, in the session).
* [Redirect the user](https://craftcms.com/docs/4.x/extend/controllers.html#redirecting-the-request) to that data set.

Then you only need a function in your template for datasets to check if the current visitor is allowed to view that dataset. You can make that function available through Twig with a [custom Twig extension](https://craftcms.com/docs/4.x/extend/extending-twig.html#register-a-twig-extension) in your module. If the visitor doesn't have permission, [exit](https://craftcms.com/docs/4.x/dev/tags.html#exit) with a 404 or 401 status.

---

You can mix and match those approaches to get the perfect solution to your problem.


---

