Routing-controllers: inject-session-object

Created on 2 May 2017  路  5Comments  路  Source: typestack/routing-controllers

Hi,
I don't understand how to inject sessions
https://github.com/pleerock/routing-controllers#inject-session-object

I've installed express-session.

import { Session } from "routing-controllers";
//import * from "express-session";
var session = require('express-session');

@Post("/login/")
loginUser(@Session() session: Express.Session, @Body() user: User) {
}

How to find such a "Express.Session" type ? How and what to import ?

question

All 5 comments

adding @19majkel94

How to find such a "Express.Session" type ? How and what to import ?

npm i -s @types/express-session

Express is a global namespace so no imports are needed.

You may also need interface merging to declare properties that you want to store in session:

declare namespace Express {
    export interface Session {
        user: any;
    }
}

Also, you need to configure the express-session middleware - this might help.

Thank you very much @19majkel94, it works fine!
I also need to use/inject in my controllers:

  • express-translate (https://github.com/uber/express-translate)
  • passport auth (http://passportjs.org/docs)
    It would be great to have a short explanation, to do that ;-)
    Whatever, thank you very much for your job !

Just use the @Req() decorator to inject the whole Request object like in normal express handler. Or write custom decorator that will extract and inject that for you 馃槈

I think this can be closed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

humbertowoody picture humbertowoody  路  4Comments

gruckion picture gruckion  路  5Comments

kkorus picture kkorus  路  4Comments

xujif picture xujif  路  6Comments

OysteinAmundsen picture OysteinAmundsen  路  3Comments