loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

A sequence action for stateful requests (HTTP sessions)

Open GuerricPhalippou opened this issue 5 years ago • 15 comments

Feature proposal

Since middlewares are internally used and not exposed to developers, there is no possible way to implement stateful requests, as it was done with the express-session middleware before v4.

Current Behavior

The v4 complying solution would be a sequence action, but it doesn't exist yet in RestBindings.SequenceActions.

Expected Behavior

Have a sequence action that retrieves cookies information and make it available to the app via context and dependency injection

GuerricPhalippou avatar Oct 16 '18 14:10 GuerricPhalippou

@GuerricPhalippou thank you for opening the issue. For reference, here is our original discussion on StackOverflow: https://stackoverflow.com/q/52833380/69868

As I wrote on StackOverflow, I think that ideally, session handling should be implemented as a new Sequence action - see Sequence docs.

class MySequence {
  // skipping constructor with dependencies to be injected
    
  async handle(context: RequestContext) {
    try {
      // the following line is new
      const session = this.restoreSession(context);

      // the default sequence continues here
      const route = this.findRoute(context.request);
      const params = await this.parseParams(context.request, route);
      const result = await this.invoke(route, params);
      await this.send(context.response, result);
    } catch (error) {
      await this.reject(context, error);
    }
  }
}

The sequence action restoreSession should:

  • check if there are any session cookies present in the request
  • load the session if a cookie was provided, maybe create a new one otherwise?
  • bind the session in our Context so that other places like controllers can receive the session via Dependency Injection

The v4 complying solution would be a sequence action, but it doesn't exist yet in RestBindings.SequenceActions. Have a sequence action that retrieves cookies information and make it available to the app via context and dependency injection.

On of our design goals for LoopBack 4 was to make it easy for 3rd party developers to contribute extensions and thus keep the framework modular. IMO, session handling should live in a new module independent from @loopback/rest.

I am expecting there will be many users looking for HTTP session support, therefore it makes sense to me to keep the session extension inside loopback-next monorepo.

@GuerricPhalippou would you like to contribute this session extension yourself? I am happy to help you along the way.

bajtos avatar Oct 18 '18 06:10 bajtos

@bajtos I'm sorry but I dropped Loopback 4 and rollbacked to Loopback 3 since the features I need will not be implemented in a near future. Plus, my employer does not allocate time for open source contribution and I feel like it takes time to dive in Loopback 4 implementation and provide a solution that covers everyone's use cases.

GuerricPhalippou avatar Oct 22 '18 10:10 GuerricPhalippou

@GuerricPhalippou Thanks for letting us know about your situation. I wish you good luck in your projects and hope to see you here again in the future, when LB4 comes closer to LB3 in terms feature parity :)

bajtos avatar Nov 23 '18 13:11 bajtos

@bajtos I want to contribute to create the new action, but actually I need a quickly solution for create the session and retrieve the sessionID, Could you please help me with that?

lazaro9318 avatar Dec 07 '18 12:12 lazaro9318

@lazaro9318 see https://github.com/strongloop/loopback-next/issues/1863#issuecomment-430896011

bajtos avatar Dec 11 '18 09:12 bajtos

@GuerricPhalippou i resolved this in the interim by using Lerna to create another Loopback 4 app package that uses Express server plus Request to get the JSESSIONID from the cookie. I then store this on Redis for any subsequent requests. I will be happy to share the solution for anyone who is deep into Loopback 4 that it’s impossible to rollback

mrmodise avatar Aug 10 '19 09:08 mrmodise

I'm interested by a solution for this feature. @mrmodise Can you share alternative ? Thanks in advance.

malek0512 avatar Feb 17 '20 12:02 malek0512

Any news ?

L-Luciano avatar Mar 03 '20 14:03 L-Luciano

Any news on this subject ?

malek0512 avatar Mar 28 '20 15:03 malek0512

Hi @L-Luciano, @malek0512, @lazaro9318

im working on an implementation for the session support. You could find an early working solution here loopback-session, would be nice know if you get it to run. Let me know if you need help.

@bajtos I'd like to contribute this package to the main mono repository. Let me know what you think of it. >Also, it would be great if someone of could help me with testing. I'm not really good at it. Thank you!

pure180 avatar May 01 '20 04:05 pure180

Please check out https://github.com/strongloop/loopback-next/pull/5118

raymondfeng avatar May 01 '20 05:05 raymondfeng

@raymondfeng thanks this look great! Will check it out later today.

pure180 avatar May 01 '20 08:05 pure180

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] avatar Dec 25 '20 14:12 stale[bot]

Is this had been solved yet ? I still didn't clear about solution how to using express-session with Loopback4

wdrdres3qew5ts21 avatar May 18 '21 07:05 wdrdres3qew5ts21

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] avatar Nov 14 '21 08:11 stale[bot]

This is still an issue. Looking the way to use express-session

shailendramonu avatar Jan 29 '23 21:01 shailendramonu

What speaks against using express-session invoked with this.expressMiddleware in your RestApplication? You can access request.session in your controllers and implement your own authentication service

damiansimon2 avatar Feb 24 '23 00:02 damiansimon2