0

I am looking to protect a cloud page with a login page. i have written this SSJS for the page to be secured:

<html>
<head>
  <script runat="server">
    Platform.Load("Core", "1");
    var username = Request.GetFormField("username");
    var password = Request.GetFormField("password");

    var allowedUsername = "your_username";
    var allowedPassword = "your_password";

    if (username != allowedUsername || password != allowedPassword) {
      Platform.Response.Redirect(CloudPagesURL(pageID));
    }
  </script>
</head>
<body> 
<!-- Protected content -->

and on the login page:

<html>
<head>
  <style>
    .container {
      width: 100%;
      max-width: 360px;
      margin: 0 auto;
      padding: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Login</h1>
    <form id="login-form" method="POST" action="%%=CloudPagesURL(protectedpageID)=%%">
      <label for="username">Username:</label><br>
      <input type="text" id="username" name="username" required><br><br>
      <label for="password">Password:</label><br>
      <input type="password" id="password" name="password" required><br><br>
      <input type="submit" value="Login">
    </form>
  </div>
</body>
</html>

I get a 500 error on the page that I want to be secured. Not sure what I am missing.

1 Answer 1

0

Your redirect on the processing page is calling the CloudPagesURL function with SSJS, but it's an AMPscript function (unless you have a user-created SSJS function we're not seeing). I would store the CloudPagesURL value in an AMPscript variable, pass that to an SSJS variable, and then redirect to that. Either that or do the redirect with AMPscript.

Here's an example of what this could look like:

<html>
<head>

%%[
  /* Assign the encrypted CloudPagesURL string to an AMPscript variable */
  set @cloudPage = CloudPagesURL(pageID)
]%%

  <script runat="server">
    Platform.Load("Core", "1");
    /* Pull in CloudPagesURL variable from AMPscript and assign it to an SSJS variable */
    var cloudPage = Variable.GetValue("@cloudPage");

    var username = Request.GetFormField("username");
    var password = Request.GetFormField("password");

    var allowedUsername = "your_username";
    var allowedPassword = "your_password";

    if (username != allowedUsername || password != allowedPassword) {

      /* Redirect to the CloudPagesURL variable */
      Platform.Response.Redirect(cloudPage);
    }
  </script>
</head>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .