How can I add a user using a 3rd party token sent from my mobile app with Silhouette?

23 views
Skip to first unread message

Elvis Lee

unread,
Apr 20, 2016, 3:55:54 AM4/20/16
to Silhouette
Hi guys,

I am trying to implement facebook login feature on my mobile app.

Here is what I am going to do.

1. Fetch a user's authInfo(access token, expired time, etc...) on mobile app. 
2. Send the authInfo to server.
3. server use the authInfo to retrieve user's profile from facebook, then add a new user to databse, finally return a JWT token.

I am going parse OAuth2Info for facebook in this case, so I put type parameter p.A in validate function.
I thought the p.A would be FacebookProvider.OAuth2Info In runtime and it does have json Reads, however, I got a compilation error: No Json deserializer found for type p.A. Try to implement an implicit Reads or Format for this type.

How can I parse dynamic type in this case?

Thanks

Elvis

  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())
    }
  }


Reply all
Reply to author
Forward
0 new messages