def signInWith3rdParty(provider: String) = Action.async(parse.json) { implicit request =>
(socialProviderRegistry.get[SocialProvider](provider) match {
case Some(p: SocialProvider with CommonSocialProfileBuilder) =>
request.body.validate[p.A].map { authInfo => // Error: No Json deserializer found for type p.A. Try to implement an implicit Reads or Format for this type.
for {
profile <- p.retrieveProfile(authInfo)
user <- userService.save(profile)
authInfo <- authInfoRepository.save(profile.loginInfo, authInfo)
authenticator <- env.authenticatorService.create(profile.loginInfo)
token <- env.authenticatorService.init(authenticator)
} yield Ok(Json.obj(
"result" -> true,
"token" -> token,
"user" -> user))
}.recoverTotal {
case error =>
Future.successful(Unauthorized(JsonValidationHandler.toJsValue(error)))
}
case _ => Future.failed(new ProviderException(s"Cannot authenticate with unexpected social provider $provider"))
}).recover {
case e: ProviderException =>
logger.error("Unexpected provider error", e)
Redirect(routes.ApplicationController.index())
}
}