Preferred way to get identity in error handler


#1

Hi

Wha’ts the preferred way to get an identity in an error handler (e.g. onNotAuthorized)?

Background:
I like to display a error view that shows the currently logged in user and the error.

I use the following method right now:

private def identity()(implicit requestHeader: RequestHeader): Future[Option[UserIdentity]] = {
    Try {
      val env = injector.instance[com.mohiva.play.silhouette.api.Environment[DefaultEnv]]
      implicit val req: Request[AnyContent] = Request(requestHeader, AnyContent())
      env.authenticatorService.retrieve.flatMap {
        case None => Future.successful(None)
        case Some(auth) =>
          env.identityService
            .retrieve(auth.loginInfo)
            .fallbackTo(Future.successful(None))
      }
    }.getOrElse(Future.successful(None))
  }

Best Regards
Stefan


#2

Hi,

Currently there is no other way. Back in the days, the error handler was not designed to show user information. I haven’t thought about this scenario.

Based on your question, I think this functionality would make sense. However, it has also some drawbacks if we change the error handler to support this functionality. The error handler must then be parameterized and with this change it’s no longer possible to use a single error handler to handle all types of environments. A user must then define a error handler for each environment.

It may be possible to use a single error handler by omitting a concrete type and use an underscore instead in the type declaration.

Anyway, this wouldn’t be changed for a minor release, because it changes the API. The next major release comes with Play 2.7 which is on the way. So I think this functionality could be integrated as well.

Let me know if you have other suggestions.

Best Regards
Christian