使工作 play2-auth 标准示例
Making work play2-auth standard sample
我正在努力使工作成为 play2-auth
标准样本,不包括授权 - 只是 authentication/login。
我在表单的 @helper.form
行中收到以下错误:
type mismatch; found : play.api.mvc.Action[play.api.mvc.AnyContent]
required: play.api.mvc.Call
@(form: Form[Option[session2.Account]])(implicit flash: Flash)
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
@helper.form(session2.ManageSession.authenticate) { // <-- here's the error
<h1>Sign in</h1>
@form.globalError.map { error =>
<p class="error">
@error.message
</p>
}
@flash.get("success").map { message =>
<p class="success">
@message
</p>
}
<p>
<input type="email" name="email" placeholder="Email" id="email"
value="@form("email").value">
</p>
<p>
<input type="password" name="password" id="password" placeholder="Password">
</p>
<p>
<button type="submit" id="loginbutton">Login</button>
</p>
}
</body>
</html>
这是ManageSession.scala:
package session2
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import jp.t2v.lab.play2.auth._
object ManageSession extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> text, "password" -> text)(Account.authenticate)
(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(views.html.login(loginForm))
}
def logout = Action.async { implicit request =>
// do something...
gotoLogoutSucceeded
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(views.html.main())),
user => gotoLoginSucceeded(user.get.email)
)
}
}
您目前正在向表单模板助手传递 authenticate
的 Action
实例,而不是 Call
instance that should be generated by its reverse route。 Call
描述了一个动作的 URL 和方法,正如你的路由文件中定义的那样,所以你不必在任何地方都对其进行硬编码。
首先,将您的 ManageSession
控制器放入 controllers
包中(根据 Play 的默认设置 - 您可以稍后移动它,但 我认为 反向路由生成依赖于它们在那里或在子包中。):
package controllers
...
object ManageSession extends Controller with LoginLogout with AuthConfigImpl {
...
}
您的 conf/routes
文件应如下所示:
POST /authenticate controllers.ManageSession.authenticate
然后在controllers.routes.ManageSession.authenticate
中生成反向路由Call
对象:
@helper.form(controllers.routes.ManageSession.authenticate) {
...
}
注意里面的routes
子包!
结果应该是HTML这样生成的:
<form method="POST" action="/authenticate">
...
</form>
注意:在 Play 2.5 及更高版本中,不鼓励使用静态(对象)控制器,并且不能与默认(注入的)路由生成器一起使用。在这种情况下,最简单的选择就是将 ManageSession
设为 class 而不是对象:
class ManageSession() extends Controller with LoginLogout with AuthConfigImpl {
...
}
或者,您可以指定 StaticRoutesGenerator
在 build.sbt
:
routesGenerator := StaticRoutesGenerator
我正在努力使工作成为 play2-auth
标准样本,不包括授权 - 只是 authentication/login。
我在表单的 @helper.form
行中收到以下错误:
type mismatch; found : play.api.mvc.Action[play.api.mvc.AnyContent] required: play.api.mvc.Call
@(form: Form[Option[session2.Account]])(implicit flash: Flash)
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
@helper.form(session2.ManageSession.authenticate) { // <-- here's the error
<h1>Sign in</h1>
@form.globalError.map { error =>
<p class="error">
@error.message
</p>
}
@flash.get("success").map { message =>
<p class="success">
@message
</p>
}
<p>
<input type="email" name="email" placeholder="Email" id="email"
value="@form("email").value">
</p>
<p>
<input type="password" name="password" id="password" placeholder="Password">
</p>
<p>
<button type="submit" id="loginbutton">Login</button>
</p>
}
</body>
</html>
这是ManageSession.scala:
package session2
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import jp.t2v.lab.play2.auth._
object ManageSession extends Controller with LoginLogout with AuthConfigImpl {
val loginForm = Form {
mapping("email" -> text, "password" -> text)(Account.authenticate)
(_.map(u => (u.email, "")))
.verifying("Invalid email or password", result => result.isDefined)
}
def login = Action { implicit request =>
Ok(views.html.login(loginForm))
}
def logout = Action.async { implicit request =>
// do something...
gotoLogoutSucceeded
}
def authenticate = Action.async { implicit request =>
loginForm.bindFromRequest.fold(
formWithErrors => Future.successful(BadRequest(views.html.main())),
user => gotoLoginSucceeded(user.get.email)
)
}
}
您目前正在向表单模板助手传递 authenticate
的 Action
实例,而不是 Call
instance that should be generated by its reverse route。 Call
描述了一个动作的 URL 和方法,正如你的路由文件中定义的那样,所以你不必在任何地方都对其进行硬编码。
首先,将您的 ManageSession
控制器放入 controllers
包中(根据 Play 的默认设置 - 您可以稍后移动它,但 我认为 反向路由生成依赖于它们在那里或在子包中。):
package controllers
...
object ManageSession extends Controller with LoginLogout with AuthConfigImpl {
...
}
您的 conf/routes
文件应如下所示:
POST /authenticate controllers.ManageSession.authenticate
然后在controllers.routes.ManageSession.authenticate
中生成反向路由Call
对象:
@helper.form(controllers.routes.ManageSession.authenticate) {
...
}
注意里面的routes
子包!
结果应该是HTML这样生成的:
<form method="POST" action="/authenticate">
...
</form>
注意:在 Play 2.5 及更高版本中,不鼓励使用静态(对象)控制器,并且不能与默认(注入的)路由生成器一起使用。在这种情况下,最简单的选择就是将 ManageSession
设为 class 而不是对象:
class ManageSession() extends Controller with LoginLogout with AuthConfigImpl {
...
}
或者,您可以指定 StaticRoutesGenerator
在 build.sbt
:
routesGenerator := StaticRoutesGenerator