import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
import { Request } from "express";

/**
 * Replaces the old app's validate_session() (src/core/auth.php), which every
 * page had to remember to call manually. Here it's a guard attached via
 * @UseGuards(AuthenticatedGuard) — a controller that forgets it simply has
 * no route, instead of silently serving unauthenticated data.
 */
@Injectable()
export class AuthenticatedGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const request = context.switchToHttp().getRequest<Request>();
    return request.isAuthenticated();
  }
}
