Silhouette provides event handling based on Akka’s Event Bus. The following events are provided by Silhouette, although only the three marked events are fired from core.

  • SignUpEvent
  • LoginEvent
  • LogoutEvent
  • AuthenticatedEvent *
  • NotAuthenticatedEvent *
  • NotAuthorizedEvent *

It is very easy to propagate your own events over the event bus by implementing the SilhouetteEvent trait.

case class CustomEvent() extends SilhouetteEvent

Use the event bus

The event bus is available in every Silhouette controller using the environment variable env.eventBus. You can also inject the event bus into other classes like services or DAOs.
EventBus() is a singleton you can call to ensure that you always use the same event bus.

Listen for events

To listen for events you must implement a listener based on an Actor and then register the listener, with the event to listen, on the event bus instance:

val listener = system.actorOf(Props(new Actor {
  def receive = {
    case e @ LoginEvent(identity: User, request) => println(e)
    case e @ LogoutEvent(identity: User, request) => println(e)
  }
}))

val eventBus = EventBus()
eventBus.subscribe(listener, classOf[LoginEvent[User]])
eventBus.subscribe(listener, classOf[LogoutEvent[User]])

Publish events

Publishing events is also simple:

val eventBus = EventBus()
eventBus.publish(LoginEvent[User](identity, request))

What’s Next