Why does the authentication directive lead to the error "Error: type of mismatch"?

I get this error in my spray project.

Error:(41, 28) type mismatch;
 found   : spray.routing.authentication.ContextAuthenticator[co.s4n.authentication.entities.Usuario]
    (which expands to)  spray.routing.RequestContext => scala.concurrent.Future[scala.util.Either[spray.routing.Rejection,co.s4n.authentication.entities.Usuario]]
 required: spray.routing.directives.AuthMagnet[?]
              authenticate(validateToken) {
                           ^

This is my TokenValidator trait.

trait TokenValidator {

  def validateToken: ContextAuthenticator[Usuario] = {
    ctx =>
      val header = ctx.request.headers.find(_.name == "Access_Token")
      if (header isDefined) {
        doAuth(header.get)
      }
      else {
        Future(Left(AuthenticationFailedRejection(AuthenticationFailedRejection.CredentialsMissing, List())))
      }
  }

  def doAuth(header: HttpHeader): Future[Authentication[Usuario]] = {
    Dao.validateToken(header.value).map {
      case Some(usuario) => Right(usuario)
      case None => Left(AuthenticationFailedRejection(AuthenticationFailedRejection.CredentialsRejected, List()))
    }
  }


}

and this is the line where I¡m gets this error

//@DELETE
  //localhost:9090/authenticacion/users/{{userEmail}}
  val `users/{{email}}` =
    pathPrefix(`path-prefix`) {
      pathPrefix(`users-path-prefix` / Segment) {
        emailRef => {
            delete {
              authenticate(validateToken) { **HERE!!!!**
                usuario =>
                  .....
              }
            }
        }
      }
    }

Does anyone know what I'm doing wrong?

I ask you all in advance!

+4
source share
2 answers

The only thing that was missing for me was that ExecutionContextin scope it should import ExecutionContext.Implicits.globalwork fine.

This will let you Futurework because it declares an implicit parameter ExecutionContext.

+11
source

I know this for a long time since the actual question appeared, but for this you can use the execution context with the tools that Spray provides:

implicit def executionContext = actorRefFactory.dispatcher
0

Source: https://habr.com/ru/post/1541175/


All Articles