play-authenticate icon indicating copy to clipboard operation
play-authenticate copied to clipboard

Using Scala

Open tabdulradi opened this issue 11 years ago • 7 comments

Hi, I wrote a Scala trait that should make life easier for Scala developers.

Please check Test.scala and Authentication.scala

tabdulradi avatar May 13 '13 16:05 tabdulradi

I am trying to use play-authenticate and I keep coming across issues because of the java orientation of this module. Is there any chance you have a working sample of a scala project? I feel like reinventing the wheel! Thank you

pligor avatar May 15 '13 07:05 pligor

The "Authentication.scala" should be enough for basic usage (getting the current user, and protecting actions from guests), also "Test.scala" contains usage samples. You are welcomed to add more helpers to the trait.

tabdulradi avatar May 15 '13 09:05 tabdulradi

So does this library to work at all if you are using scala with the playframework?

sherzberg avatar May 22 '13 20:05 sherzberg

Well, my plan was to use the sample usage app as it is, and build my own Scala controllers on top of it, using the model and the session. I had problems because Play provides different classes to deal with sessions, "play.mvc.Http.Session" for Java and "play.api.mvc.Session" for Scala. So I had to re-implement some methods from Play-Authenticate, like "getUser". Also had some problems with null values, instead of the great Scala Options. Probably you will get into similar troubles if you used it further, but all problems all are solvable for me till now.

tabdulradi avatar May 23 '13 09:05 tabdulradi

I am trying to use play-authenticate from Scala as well. How did you work around the session issues? I am getting the following runtime exception when using @currentAuth() { auth => ... } in a template:

play.api.Application$$anon$1: Execution exception[[RuntimeException: There is no HTTP Context available from here.]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.2]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.2]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:165) [play_2.10.jar:2.2.2]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$12$$anonfun$apply$1.applyOrElse(PlayDefaultUpstreamHandler.scala:162) [play_2.10.jar:2.2.2]
        at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:33) [scala-library.jar:na]
        at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:185) [scala-library.jar:na]
Caused by: java.lang.RuntimeException: There is no HTTP Context available from here.
        at play.mvc.Http$Context.current(Http.java:30) ~[play_2.10.jar:2.2.2]
        at play.mvc.Http$Context$Implicit.session(Http.java:182) ~[play_2.10.jar:2.2.2]
        at com.feth.play.module.pa.views.html.currentAuth$.apply(currentAuth.template.scala:33) ~[play-authenticate_2.10-0.5.2-SNAPSHOT.jar:0.5.2-SNAPSHOT]
        at views.html.index$.apply(index.template.scala:45) ~[classes/:na]
        at controllers.Pages$$anonfun$index$1.apply(Pages.scala:34) ~[classes/:na]
        at controllers.Pages$$anonfun$index$1.apply(Pages.scala:33) ~[classes/:na]

rocketraman avatar Apr 18 '14 01:04 rocketraman

@rocketraman ( and anyone after him that finds this post and has the same problem. )

The following quoted scala file will make an action that will have the http context.

import play.api.mvc._
import scala.concurrent.Future
import play.mvc.Http.Context
import play.core.j.JavaHelpers

object ContextAction extends ActionBuilder[Request] {

  def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
    Context.current.set(JavaHelpers.createJavaContext(request))
    block(request)
  }
}

http://stackoverflow.com/questions/17230010/using-scala-and-java-in-play-framework-2-1-session-usage

The problem I (and rocketraman ) had only happens when one, instead of text such as in the Test.scala file above, try to render a html page.

ZeerDonker avatar Aug 26 '14 12:08 ZeerDonker

in scala: http://securesocial.ws/

dportabella avatar Nov 17 '14 19:11 dportabella