I would like to have access to the current user somewhere deep in my models of my Play app, for things like setting the author, checking that the user can actually save this type, etc.
Ideally, what I would like to use is Guice's
@RequestScoped to inject the same
UserIdentity across my request, wherever I need it. However, as far as I can tell, the Play! Framework only supports
@Singleton and no-scope. So either we'd get the same
UserIdentity injected across requests or a different one for every model/util we requested. Both are no-gos for obvious reasons.
Is there some way to utilise this behaviour in Play 2.5?
<strong>Other things I have tried</strong>
I've tried using a combination of Play's session and cache. But the problem I have is that session is immutable, so I can't add anything to it to reuse in that same request.
I've looked at a bunch of auth frameworks but they all seem to focus on securing actions, not providing me with a current User object.Answer1:
Check out <a href="https://stackoverflow.com/questions/36797048/how-to-redirect-request-in-play-framework-with-scala/36797143#36797143" rel="nofollow">my answer to a question on redirecting requests</a>, where I given an example of getting the current user in each request.
In this case the authorization key is handed out on login and the client passes it in on every request thereafter.