Question

I started customizing securesocial for my own use and encountered one problem during customization of my views - I am trying to make persistent toolbar template on top of the window with text like:

Welcome,
@if(user != null) {
  @user.firstName @user.lastName
} else {
  @Messages("Guest")
}

This toolbar is generated in custom Main view:

@(title: String, user: securesocial.core.Identity = null)(content: Html)

Which is invoked from code (index.scala.html example):

@main("App test",user) 

I encounter problem when trying to use customized views - especially passwordChange.scala.html. This view is invoked from standard controller provided by securesocial (PasswordChange) which then passes control to my own plugin (MyViews, which has simple implementation of rendering views and extends TemplatesPlugin), doesn't have any information about user context - so even if the user is logged on, during password change, toolbar will display "Welcome, guest" and corresponding menus will show e.g 'Login' and 'Signup' despite the fact user is logged on during password change.

Can anybody provide me with solution to pass user info to my custom view (preferably without rewriting securesocial built in controllers)?

Was it helpful?

Solution 2

The solution was very simple and sleek - instead of populating user from HTTP Context object (which was unavailable in this part of code), I populated user from implicit request and then passed variable to my custom template:

  def getPasswordChangePage[A](implicit request: SecuredRequest[A], form: Form[ChangeInfo]): Html = {
    val userName = request.user
    views.html.secure.Registration.passwordChange(request, userName, form)
  }

The code above is part of MyViews.scala code.

OTHER TIPS

Looks like user is null. Are you populating it in your code?

You also need to add a query in your user model to find it. For example check this:

Create model class in Scala with Play 2 Framework

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top