Giter Site home page Giter Site logo

custom header about akka-http-session HOT 5 CLOSED

fintis avatar fintis commented on July 18, 2024
custom header

from akka-http-session.

Comments (5)

adamw avatar adamw commented on July 18, 2024

Are you using the correct transport in the directives, e.g. setSession(oneOff, usingHeaders, sessionContent)?

from akka-http-session.

fintis avatar fintis commented on July 18, 2024

Hi,

Here is a code snippet showing how I have implemented...

trait RestApi extends RepositorySupport {
  implicit val system = GlobalInjector.getInjector.instance[ActorSystem]
  implicit val timeout = Timeout(60.seconds)

  val sessionConfig = SessionConfig.fromConfig()
  implicit val sessionManager = new SessionManager[ScSession](sessionConfig)

  implicit val refreshTokenStorage = new RefreshTokenStorage[ScSession] {
    override def lookup(selector: String): Future[Option[RefreshTokenLookupResult[ScSession]]] = {

      tokenRepo.tokenService.findSecureTokenBySelector(selector)
    }

    override def schedule[S](after: Duration)(op: => Future[S]): Unit = {
      ???
    }

    override def store(data: RefreshTokenData[ScSession]): Future[Unit] = {
      val token = Token(secureToken = Some(SecureToken(session = data.forSession, selector = data.selector, tokenHash = data.tokenHash, expires = data.expires)))
      tokenRepo.tokenService.createSecureToken(token)
    }

    override def remove(selector: String): Future[Unit] = {
      tokenRepo.tokenService.deleteSecureTokenBySelector(selector)
    }
  }
  def mySetSession(v: ScSession) = {
    println(s"setting session ${v.token}")
    setSession(refreshable, usingHeaders, v)
  }

  val myRequiredSession = requiredSession(refreshable, usingHeaders)
  val myInvalidateSession = invalidateSession(refreshable, usingHeaders)

  def serviceManager(implicit system: ActorSystem) = system.actorOf(Props[ServiceManager])


}

The trait above is inherited in the route as below

trait AuthRoutes extends CORSSupport with RestApi {


  def authRoutes(implicit executionContext: ExecutionContext): Route = {
    cors {
      pathPrefix("api" / "v1") {
        (post & path("login" / Segment)) { user =>
          entity(as[LoginDTO]) { login =>
            complete {
              serviceManager.ask(Authenticate(login, user)).mapTo[LoginMessage].map {
                case msg: LoginSuccess => mySetSession(ScSession(msg.token))
                  HttpResponse(200, entity = msg.token)
                case LoginFailure => HttpResponse(400, entity = "Bad request")
                case _ => HttpResponse(400, entity = "Bad request")
              }
            }
          }
        } ~

I am sorry for the late response...

Many thanks for your help

from akka-http-session.

adamw avatar adamw commented on July 18, 2024

I'm not sure if I'm reading your code correctly but mySetSession is a directive and should be used as such; it doesn't have any side-effects, it needs to wrap other handlers that should complete the request.

I think you are just calling mySetSession and the returning the response HttpResponse as two separate statements, so the results of mySetSession are ignored.

I think the correct usage would be:

onSuccess(serviceManager.ask(Authenticate(login, user)).mapTo[LoginMessage]) {
  case msg: LoginSuccess => 
    mySetSession(ScSession(msg.token)) {
      complete(HttpResponse(...))
    }
  case ... => complete(...)
}

from akka-http-session.

fintis avatar fintis commented on July 18, 2024

@adamw oaf! I think you are right. Didn't reason it that way.. I will give it a try and let you know how it goes.

from akka-http-session.

fintis avatar fintis commented on July 18, 2024

@adamw Many thanks for your prompt intervention... 👍

Still don't know why i didn't use it as a directive as it was very clear in the example.

I guess I got carried away with the ask pattern viz-a-viz. I will close now!

Cheers

from akka-http-session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.