import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { RequestWithSession } from './controller/auth.controller';

@Injectable()
export class SessionGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest<RequestWithSession>();
    const sessionIndex = request.headers['sessionindex'] as string; // Retrieve sessionIndex from headers
    //const sessionUser = request.session.user;
    if (sessionIndex) {
      return true;
    } else {
      request.res.redirect('/login');
      return false;
    }
  }
}
