Play Session information won't persist across requests - trouble with implicits?

86 views Asked by At

I have no idea why, this wasn't a problem until today, and I hadn't changed any code in this controller in a while, but I suddenly can't seem to save information across requests in my app made with Play 2.6. In my LoginController, I have this method called from a POST request when a user fills out their credentials in a Form:

def attemptLogin() = Action {
   implicit request: Request[AnyContent] => {
    loginform.bindFromRequest().fold(
      formWithErrors => BadRequest(views.html.login(formWithErrors)),
      successfulForm => {
        val validator: DbInputValidator = ... // validation object, stores Form info
        if (validator.inputsAreValid) {
          val userName = ... // retrieves user name from Form info
          Ok(views.html.loginSuccess())
            .withSession(request.session + ("authenticated" -> "true") + ("username" -> userName))
            .withHeaders(SecurityHeadersFilter
              .CONTENT_SECURITY_POLICY_HEADER -> " .fontawesome.com .fonts.googleapis.com maps.googleapis.com")
        } else {
          BadRequest(views.html.login(loginform))
        }
      }
    )
  }
}

And the view.html.successLoginResponse I have load is the following:

@()(implicit request: RequestHeader)

@getAuthAndUserName = @{
    val foundUsername: String = request.session.get("username").getOrElse("no username found")
    val foundAuthenticated: String = request.session.get("authenticated").getOrElse("No authentication found")
    s"$foundUsername and $foundAuthenticated"
}

@main("Successful Login") {

    @landingnavbar("login")

    <div class="container">

                    <h1>Login Successful</h1>
                    <h2>The auth token and username are:</h2>
                    <h2 id="headerValues">@getAuthAndUserName</h2>
                    <h2>Redirecting you to the Dashboard in...</h2>
                    <h3>
                        <div id="counter">3</div>
                    </h3>


    </div>

    <!-- JS here which automatically redirects to a URL after countdown -->
    <script>...</script>
}

But when this loads, the values retrieved by the method I have in the view are the "or else" results of "no username found" and "no authentication found"

So clearly, the OK request returned is not storing the information, even though I do appear to add them in the .withSession(request.session + ("authenticated" -> "true") + ("username" -> userName))

line in the code. Before today, it would do this and not display the information back to me, but then on any subsequent page load, that request would retrieve the data, and I would be able to grab the username or the authentication check. Is it grabbing the implicit request at the top and loading that into the view? From the old version of the request that first called attemptLogin()? If so, how do I get it to take the new session data instead?

I tried doing

request.session + ("authenticated" -> "true")
request.session + ("username" -> userName)

Just before the Ok(...) response, but that didn't take.

0

There are 0 answers