How to get user data from session passport js?

43 views Asked by At

so I have a nestjs application with session authentication from passport js. Then I want to access user data from the session for the need to check existing roles like this

@Injectable()
export class RoleGuard implements CanActivate {

  constructor(private reflector: Reflector){}

  canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
    const requiredRole = this.reflector.getAllAndOverride<Roles[]>('role', [
      context.getHandler(),
      context.getClass()
    ]);

    if(!requiredRole || requiredRole.length === 0) return true; //if there is no role, then the route will be considered public

    const { session } = context.switchToHttp().getRequest<Request>();

    return requiredRole.some((role) => session.user.role.includes(role));
                                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  }
}

And then i got an error like this : Property 'user' does not exist on type 'Session & Partial<SessionData>'.

so, how do I get the user data from session??

I wanted to check the user data from session and the relation of role.

0

There are 0 answers