Show loggedin User Information in the title of my web application every time

I am working on game 2.1 with reliable social integration. At the moment, I was able to integrate securesocial with mongod db using SALAT. Now I can log in / log out. But now what I want is if the user is logged in, then I need to show user information, such as an avatar, etc. The title of my web application, and I'm not sure how to get user information in scala.html without passing as params from Controller. I can not do it every time.

We have something similar to spring security that grabs a user from a session and uses EL or spring taglib to display user information ???

+4
source share
3 answers

It is sad that this community is so inactive or does not want to answer this question. I still find spring and java communities that answer the most basic questions even after a few years. So here is what I did. All improvements or suggestions are welcome.

Wrote a method in my controller that will return the user as an implicit method.

implicit def user(implicit request: RequestHeader):Option[Identity] = { SecureSocial.currentUser } 

Pass this user into my template and make it implicit. I wonder why he cannot use it directly on the controller. I must explicitly convey this, which is very strange.

  @(implicit user:Option[securesocial.core.Identity]) 

Since user information must be on all pages, it must be included in the main template or main template, calling another template that displays user information. In my main template =>

 @(title: String, nav: String = "")(content: Html)(implicit user:Option[securesocial.core.Identity]=None) 

Then some kind of related code

 @if(user.isDefined){ <li> <a href="@securesocial.controllers.routes.LoginPage.logout()"/>Logout</li> }else{ <li> <a href="@securesocial.core.providers.utils.RoutesHelper.login()">Login</a></li> } 
+3
source

You can call the controller method in the template to get something without passing it as a parameter:

 @defining(Auth.getCurrentUserName()) {user => <div>Hello, @user</div> } 

Auth is the controller, getCurrentUserName() just gets some data from the session.

 public static String getCurrentUserName() { return session().get("username"); } 
+2
source

To make things easier without providing an implicit user, simply call SecureSocial.currentUser () directly in the scala template.

You need to import it, and then you can check if the user is defined:

 @import securesocial.core.SecureSocial @if(SecureSocial.currentUser.isDefined) { ... } 
+2
source

Source: https://habr.com/ru/post/1498285/


All Articles