nullox
nullox

Reputation: 395

get user information from views with securesocial in play framework

I have a main.html in views folder. I want to get the currently logged on user info to the view but i dont want to pass it by parameter. Is there a way to do so?

main.html

 @(title: String)(content: Html)
 <html>
    <head></head>
    <body>Welcome {User}!</body>
 </html>

Upvotes: 1

Views: 143

Answers (2)

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

You can create an object with a method that will extract a user from a request.

object UserInfo {
  def getCurrentUser(implicit request: RequestHeader): Option[Identity] = {
    request match {
      case r: SecuredRequest[_] => Some(r.user)
      case r: RequestWithUser[_] => r.user
      case _ => None
    }
  }
}

Then in a view you can simply use this method statically.

@(title: String)(content: Html)(implicit request: RequestHeader)
@import app.auth.UserInfo
<html>
    <head></head>
    <body>Welcome @UserInfo.getCurrentUser !</body>
 </html>

Upvotes: 1

Ryan
Ryan

Reputation: 7247

No, whatever you need in the view needs to be passed in from the controller.

Upvotes: 0

Related Questions