1

I am working on a LoopBack 4 application and I need to persist sessions. I have read that one way to do this is by using the RequestWithSession interface, which extends the built-in Express.Request interface with an additional session property. However, I am not sure how to use this interface.

Here's an example of my sign in function:

import {RequestWithSession, RestBindings, requestBody} from '@loopback/rest';

 async signIn(
    @requestBody(RequestBody)
    credentials: Credentials,
    @inject(RestBindings.Http.REQUEST) request: RequestWithSession,
  ): Promise<{ token: string }> {
    const user = await this.userService.verifyCredentials(credentials);
    const userProfile = this.userService.convertToUserProfile(user);
    const token = await this.jwtService.generateToken(userProfile);
    request.session.user = userProfile;
    return { token, ...userProfile };
  }

Can anyone provide an example of how to use the RequestWithSession interface to persist sessions in a LoopBack 4 application? Are there any documentation or tutorials available that explain how to use this interface effectively? My goal is not necessarily to use RequestWithSession, any tool that gives me a session will do.

Thank you in advance for your help.

Console.log of request.session gives me undefined

rkoikai
  • 11
  • 3

0 Answers0