I have written a scala method to do authentication and save a specific token in session like this:
def logIn = Action(parse.json) { request =>
request.body.validate.map { entity =>
Authentication.authenticate(entity.username, entity.password) match {
case "" => NotFound(RestResponses.toJson(RestResponse(NOT_FOUND, "Invalid username or password.")))
case token: String => Ok(RestResponses.toJson(RestResponse(OK, "Username %s has been succesfully logged in".format(entity.username)))).withSession(TokenKey -> token)
}
}
.recover { Result =>
BadRequest(RestResponses.toJson(RestResponse(BAD_REQUEST, "Unable to parse content type for user authentication.")))
}
}
To test logIn method I have written a test method:
"A POST request to login a user" should "return OK " in {
running(FakeApplication()) {
val node = Json.toJson(AuthenticationUser("cristi", "cristi-password"))(controllers.AuthenticationService.authUserWrites);
val result = AuthenticationService.logIn(new FakeRequest(Helpers.POST, "/", FakeHeaders(), node))
status(result) should equal(OK)
contentType(result) should be(Some("application/json"))
contentAsString(result) should include("succesfully logged in")
}
}
I simply don’t understand why I receive this error:
[info] - should return OK *** FAILED ***
[info] play.api.PlayException: Configuration error[Missing application.secret]
[info] at play.api.libs.Crypto$$anonfun$sign$2.apply(Crypto.scala:30)
[info] at play.api.libs.Crypto$$anonfun$sign$2.apply(Crypto.scala:30)
I am sure it’s something about session… but what?
Add a
application.secret=changemeto yourconf/application.conffile.