Check session variable before delivering cached page

@odessite‍  i think you missunderstood the whole 3rd party signup. 3rd party is delivering batches of users that are beeing imported to a custom table in shopware database. So the login is just a matter of running a select query and checking if user with this credentials exists in a custom table in shopware database. This table however does not have any data that shopware customer needs (shipping address, email address, title, name ,…)

there is no need to contact the 3rd party service on login itself, because the data from it is beeing imported to the shopware database.
this all works already.

all what I want is to have the option to say something in the lines of this pseudo code

if ($_SESSION['passengerId'] === null && url !== "login") {
    redirect to login page
}

and this 3 lines of code should run somewhere where session is already available, but before page is loaded from cache