Closed davidquintard closed 6 years ago
Hi there, i'm using express-session with routing-controllers and i can't get data from session.
var options = { host: process.env.MARIADB_HOST, port: process.env.MARIADB_PORT, user: process.env.MARIADB_USER, password: process.env.MARIADB_PASS, database: process.env.MARIADB_NAME }; var sessionStore = new MySQLStore(options); expressApp.use(session({ key: 'session_cookie_name', secret: 'session_cookie_secret', store: sessionStore, resave: false, saveUninitialized: false }));
@Post("/login") login(@Session() session: Express.Session, @Body() body: any, @Req() request: Request) { .... const authService = Container.get(AuthService); return authService.save(membre, session); }
save(membre: Membre, session: Express.Session) { .... session.membre = {} session.membre['id'] = membre.getId(); .... }
After that, session is correctly stored in database:
{"cookie":{"originalMaxAge":null,"expires":null,"secure":false,"httpOnly":true,"path":"/","sameSite":false},"membre":{"id":204658}}
But when i call another page like this:
@Get("/liste/skip/:skip/take/:take") liste(@Param("skip") skip: number, @Param("take") take: number, @Session() session: Express.Session, @Req() request: Request) { console.log(session) return this.candidatRepository.createQueryBuilder("c") .leftJoinAndSelect('c.annonce', 'ann') .leftJoinAndSelect('c.adresse', 'adr') .innerJoinAndSelect('adr.country', 'co') .leftJoinAndSelect('c.galeriesPhoto', 'gal', 'gal.acces=1') .leftJoinAndSelect('gal.photos', 'cp', 'cp.ordre=1') .where("c.membre=:membre") .andWhere("c.id_site=:id_site") .setParameter("membre", session.membre.id) => TypeError: Cannot read property 'id' of undefined .setParameter("id_site", Container.get(ConfigService).get('HOSTID')) .skip(skip) .take(take) .getMany() .then(function(candidatures) { return candidatures; }) .catch(function(error) { return { 'error': i18n.__("Une erreur est survenue! ") // an error occured }; }); }
... session is not retrieved (TypeError: Cannot read property 'id' of undefined)
console.log(session) :
Session { cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true, secure: false, sameSite: false } }
This error occurs when my ionic app calls my express api by ajax xmlHttpRequest. But when i call this api directly from my browser: http://localhost:3000/candidat/candidature/liste/skip/0/take/20 It works fine, i get response without error.
Did i miss something ?
When making the AJAX request, make sure to set the withCredentials property to true.
Hi there, i'm using express-session with routing-controllers and i can't get data from session.
After that, session is correctly stored in database:
But when i call another page like this:
... session is not retrieved (TypeError: Cannot read property 'id' of undefined)
console.log(session) :
This error occurs when my ionic app calls my express api by ajax xmlHttpRequest. But when i call this api directly from my browser: http://localhost:3000/candidat/candidature/liste/skip/0/take/20 It works fine, i get response without error.
Did i miss something ?