title: How to use a Scala Secure Trait in PlayFramework?
body: <p>I'm trying to build a web application in Scala using <a href="http://scala.playframework.org/" rel="noreferrer">Play Framework</a>. When using Play Framework in Java I can use the <a href="http://www.playframework.org/documentation/1.2.1/secure" rel="noreferrer">Secure module</a> to do authentication for pages that require logins. This is a common problem in many web applications, and I would like to use a general solution for my web application.</p>
  
  <p>I have tried to follow <a href="http://scala.playframework.org/documentation/scala-0.9.1/controllers#MixingcontrollersusingTraits" rel="noreferrer">Mixing controllers using Traits</a> with a Secure trait example, but my trait doesn't even compile, and I don't understand what's wrong.</p>
  
  <p>I have created the trait from the example and saved it on <code>mysite\app\Secure.scala</code>:</p>
  
  <pre><code>package controllers
  
  import play._
  import play.mvc._
  
  trait Secure {
      self:Controller =&gt;
  
      @Before checkSecurity = {
          session("username") match {
              case Some(username) =&gt; renderArgs += "user" -&gt; User(username)
                                     Continue
              case None =&gt; Action(Authentication.login)
          }
      }
  
      def connectedUser = renderArgs("user").get
  
  }
  </code></pre>
  
  <p>Then I use the Secure trait in a simple <code>mysite\app\MySecretController.scala</code>:</p>
  
  <pre><code>package controllers
  
  import play._
  import play.mvc._
  
  object MySecretController extends Controller with Secure {
      def index = &lt;h1&gt;Hello&lt;/h1&gt;
  }
  </code></pre>
  
  <p>But when visiting the page I get Compilation error:</p>
  
  <pre><code>The file /app/Secure.scala could not be compiled. Error raised is : expected 
  start of definition
  </code></pre>
  
  <p>on this line:</p>
  
  <pre><code>@Before ↓checkSecurity = {
  </code></pre>
  
  <p>I also created a simple <code>mysite/app/User</code> class:</p>
  
  <pre><code>package controllers
  
  class User (name: String){
  
  }
  </code></pre>
  
  <p>Any suggestion on how I can solve this?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>After adding <code>def</code> as suggested by Felipe. I get another error <code>not found: value User</code> on:</p>
  
  <pre><code>case Some(username) =&gt; renderArgs += "user" -&gt; ↓User(username)
  </code></pre>