How to connect Web socket + JWT


#1

I used JWT to authenticate web socket. But i don’t how to connect it. My front-end was used React. I can’t use header in making ws connection .

My socket code:

def socket = WebSocket.acceptOrResult[JsValue, JsValue] { request =>
  implicit val req = Request(request, AnyContentAsEmpty)
  silhouette.SecuredRequestHandler { securedRequest =>
    Future.successful(HandlerResult(Ok, Some(securedRequest.identity)))
  }.map {
    case HandlerResult(r, Some(user)) => Right(ActorFlow.actorRef(ChatActor.props(user.copyToUserRow)))
    case HandlerResult(r, None) => Left(r)
  }
}

Router:

GET         /ws                  controllers.ChatApi.socket

Javascript:

var token = "Token"
var options = {
  headers: {
    "X-Auth-Token" : token
  }
};

var socket = new WebSocket("ws://localhost:9000/ws",[], options);

#2

Hi,

have you read: Silhouette + Websockets + JWT

Best regards,
Christian