Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does authenticate directive lead to "Error: type mismatch"?

Tags:

spray

I'm getting 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 getting that 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 am I doing wrong?

Thak you all in advance!

like image 212
Rodrigo Cifuentes Gómez Avatar asked Dec 20 '22 14:12

Rodrigo Cifuentes Gómez


1 Answers

The only thing I was missing was to have ExecutionContext in scope and import ExecutionContext.Implicits.global worked fine.

It's to let Futures work as they declare an implicit ExecutionContext parameter.

like image 84
Rodrigo Cifuentes Gómez Avatar answered Apr 27 '23 20:04

Rodrigo Cifuentes Gómez