def user_authentication(request: Request):
    """
    Authenticates the user based on the following priority:
    1. HF_TOKEN environment variable
    2. OAuth information in session
    3. Token in bearer header (not implemented in the given code)

    Args:
        request (Request): The incoming HTTP request object.

    Returns:
        str: The authenticated token if verification is successful.

    Raises:
        HTTPException: If the token is invalid or expired and the application is not running in a space.

    If the application is running in a space and authentication fails, it returns a login template response.
    """
    # priority: hf_token env var > oauth_info in session > token in bearer header
    # if "oauth_info" in request.session:
    if HF_TOKEN is not None:
        try:
            _ = token_verification(token=os.environ.get("HF_TOKEN"))
            return HF_TOKEN
        except Exception as e:
            logger.error(f"Failed to verify token: {e}")
            if IS_RUNNING_IN_SPACE:
                return templates.TemplateResponse("login.html", {"request": request})
            else:
                raise HTTPException(
                    status_code=status.HTTP_401_UNAUTHORIZED,
                    detail="Invalid or expired token: HF_TOKEN",
                )

    if IS_RUNNING_IN_SPACE and "oauth_info" in request.session:
        try:
            _ = token_verification(token=request.session["oauth_info"]["access_token"])
            return request.session["oauth_info"]["access_token"]
        except Exception as e:
            request.session.pop("oauth_info", None)
            logger.error(f"Failed to verify token: {e}")
            return templates.TemplateResponse("login.html", {"request": request})

    if IS_RUNNING_IN_SPACE:
        return templates.TemplateResponse("login.html", {"request": request})

    raise HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Invalid or expired token",
    )

    Authenticates the user based on the following priority:
    1. HF_TOKEN environment variable
    2. OAuth information in session
    3. Token in bearer header (not implemented in the given code)

    Args:
        request (Request): The incoming HTTP request object.

    Returns:
        str: The authenticated token if verification is successful.

    Raises:
        HTTPException: If the token is invalid or expired and the application is not running in a space.

    If the application is running in a space and authentication fails, it returns a login template response.
    
src/autotrain/app/ui_routes.py