Unauthorized requests can come from multiple sources:
A logged in user which is not allowed to view a specific page
A logged in user which “times out” and gets redirected to the login page due to e.g. a not valid CookieAuth cookie
A not logged in user which tries to view a specific page which requires authentication (which basically is not 100% an authorization fail but maybe you want to catch that as well)
and maybe some more
Can you maybe explain in more detail which kind of usecase you want to catch so we can provide better support for you.
I just need to handle the first case: “a logged in user which is not allowed to view a specific page” which I would also identify as “any request where the EntityPolicy.php returned false” or "any ForbiddenException".
I think the other two would be called “an unauthenticated request” or a MissingIdentityException
With last visited page I mean “the last place the user visited and was authorized to” therefore creating the cookie in the beforeRender() method is just a partial solution.
I think I should be looking for a method called after any EntityPolicy.php method returns true.
On the other hand, reading the cookie on ForbiddenExceptionHandler class seems correct to me.
I’ll also have this piece of code which might clarify better what I’m trying to achieve.