Skip to content

Advanced usage: Detect if user is logged in

Manabu Nakamura edited this page May 11, 2013 · 2 revisions

Changing the display depending on whether the user is logged in

If you want to display the application's index differently to non-logged-in users and logged-in users, you can use OptionalAuthElement insted of AuthElement:

object Application extends Controller with OptionalAuthElement with AuthConfigImpl {

  // maybeUser is an instance of `Option[User]`.
  // `OptionalAuthElement` dont need `AuthorityKey`
  def index = StackAction { implicit request =>
    val maybeUser: Option[User] = loggedIn
    val user: User = maybeUser.getOrElse(GuestUser)
    Ok(html.index(user))
  }

}