ps0604
ps0604

Reputation: 1071

Basic authentication with play2-auth in Play 2.5.x

In the example below, the intent is to build a simple play2-auth program that authenticates a user keeps a session until they log out.

Problem is that I get the following compilation error in ManageSession.scala:

object creation impossible, since: it has 6 unimplemented members

This is the source code, I took the sample from play2-auth website and simplified, but the function signatures are the same:

AuthConfigImpl.scala:

import play.api.mvc._
import scala.reflect._
import play.api.mvc.RequestHeader
import play.api.http.Status
import play.api.mvc.Results._
import jp.t2v.lab.play2.auth._

trait AuthConfigImpl extends AuthConfig {

  type Id = String
  type User = Account
  val idTag: ClassTag[Id] = classTag[Id]
  val sessionTimeoutInSeconds: Int = 3600

  def loginSucceeded(request: RequestHeader): Result = Redirect("/main",Status.OK)
  def logoutSucceeded(request: RequestHeader): Result = Redirect("/main",Status.OK)
  def authenticationFailed(request: RequestHeader): Result = Redirect("/main",Status.OK)
  def authorizationFailed(request: RequestHeader): Result = Forbidden("no permission")
  def authorize(user: User, authority: Authority): Boolean = true

//override lazy val cookieSecureOption: Boolean = 
//  play.api.Play.current.configuration.getBoolean("auth.cookie.secure").getOrElse(true)

}

ManageSession.scala:

import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
import scala.concurrent.ExecutionContext.Implicits.global
import jp.t2v.lab.play2.auth._

object ManageSession extends Controller with LoginLogout with AuthConfigImpl {

  val loginForm = Form {
    mapping("email" -> email, "password" -> text)(Account.authenticate)
    (_.map(u => (u.email, "")))
      .verifying("Invalid email or password", result => result.isDefined)
  }

  def login = Action { implicit request =>
    Ok(views.html.showlogin())
  }

  def logout = Action { implicit request =>
    //gotoLogoutSucceeded
    Ok("logged out")
  }

  def authenticate = Action { implicit request =>
    loginForm.bindFromRequest.fold(
      formWithErrors => BadRequest(views.html.showlogin()),
      user => Ok("logged in") // gotoLoginSucceeded(user.get.email)
    )
  }
}

Account.scala:

case class Account(id: Int, email: String, password: String, name: String)

object Account  {

  def authenticate(email: String, password: String): Option[Account] = {
      if (email == "test")
          Some(Account (1, "test", "abc", "Paul"))
      else
          None
  }

Upvotes: 0

Views: 1032

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

gotoLoginSucceeded and gotoLogoutSucceeded in play2-auth return Future[Result], but Action expects a Result. You can fix this by using Action.async:

def logout = Action.async { implicit request =>
  // do something...
  gotoLogoutSucceeded
}

import scala.concurrent.Future

def authenticate = Action.async { implicit request =>
  loginForm.bindFromRequest.fold(
    formWithErrors => Future.successful(BadRequest(views.html.showlogin())),
    user => gotoLoginSucceeded(user.get.id)
  )
}

The same must be done with your AuthConfig. All of these methods are expected to return Future[Result]. For example

def loginSucceeded(request: RequestHeader): Future[Result] = 
    Future.successful(Redirect("/main", Status.OK))

And so on.

Upvotes: 1

Related Questions