moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
291 stars 119 forks source link

How to use Redis Store with Moleculer js? #157

Closed Moulali1995 closed 4 years ago

Moulali1995 commented 4 years ago

How can i implement sessions in moleculer js using express-session and redis store for saving the session data? Does express-session runs without express server in moleculer js?

icebob commented 4 years ago

Yes, you can use many express middleware in the moleculer-web API gateway, as well.

Moulali1995 commented 4 years ago

155 https://github.com/moleculerjs/moleculer-web/issues/155

I tried the middlewares but redis store is not working. At first i thought like middleware is not called then @AndreMaz told me that "express server is not needed you can directly call middlewares and use express-session with the help of middlewares no need to create and start the express server.". But when i'm trying to connect to redis it is not working and the cookie is not sent and session data is not stored but when the same is tried with express framework alone it is working.

icebob commented 4 years ago

Please create a repro repo with middlewares what you need and we will check it.

Moulali1995 commented 4 years ago

I've created a repo please check. https://github.com/Moulali1995/moleculerjs-redis-jwt-express-session.git

env variables in .env.tmpconfig

Moulali1995 commented 4 years ago

login API: JWT token is generated http://localhost:3000/auth/login

payload : { "username": "user1", "password": "admin" }

authentication API: using the above JWT token and sending it in cookie headers http://localhost:3000/api/user

unable to see any sort of session id generated in the redis cli monitor.why? In response there is no express session cookie passed generated by the middleware express-session when the same is tried in express framework it works .why?

icebob commented 4 years ago

It's not correct:

ctx.meta.$responseHeaders = { Cookie: `session_token=${data.key}` };

Please check how to set a cookie or use a lib to generate cookie string.

Moulali1995 commented 4 years ago

Thanks @icebob it worked by removing the express-session code and directly using set and get methods of redis-store and by using set-cookie method.