Last active
January 3, 2016 00:29
-
-
Save mikesname/8383440 to your computer and use it in GitHub Desktop.
Basic Play optional authenticated action.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.mvc._ | |
case class Context(email: Option[String]) | |
class AuthenticatedRequest[A](val context: Context, request: Request[A]) extends WrappedRequest[A](request) | |
object MaybeAuthenticated extends ActionBuilder[AuthenticatedRequest] { | |
def invokeBlock[A](request: Request[A], block: (AuthenticatedRequest[A]) => Future[SimpleResult]) = { | |
request.session.get("email").map { email => | |
block(new AuthenticatedRequest(Context(Some(email)), request)) | |
} getOrElse { | |
block(new AuthenticatedRequest(Context(None), request)) | |
} | |
} | |
} | |
def myAction = MaybeAuthenticated { implicit request => | |
request.context.email.map { email | |
Ok("The current user is " + email) | |
} getOrElse { | |
Ok("No logged in user") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment