I have a portal, where you can add posts. If you add a post, or the post will be changed, then you will get an email with a link to your post.
When you click on the link, it brings you directly to the post. But since you are not logged in, an error message
Identity is not authorized to perform ``view``
I mean, it is ok, if the user is not logged in.
But what I want to have is, that the user will be redirected to the login page and after login, the user should come back to the post.
Where should I implement this? In my user class? Or in the class I want to open and get the error? Or do I need to create a new class?
When someone tries to open the page without beeing logged in, then I want to show an errormessage and redirect the user to the login page.
But for now I get an error that the user object in my policy is null.
Yes you are right. I am completely confused about middleware and I was not aware that I have to do something.
I will read the documentation and hope it will be clearer then.
Thanks a lot.
<?php
declare( strict_types = 1 );
namespace App\Middleware\UnauthorizedHandler;
use Authorization\Exception\Exception;
use Authorization\Middleware\UnauthorizedHandler\RedirectHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class CustomRedirectHandler extends RedirectHandler {
public function handle( Exception $exception, ServerRequestInterface $request, array $options = [] ): ResponseInterface {
$response = parent::handle( $exception, $request, $options );
$request->getFlash()->error( 'You are not authorized to access that location' );
return $response;
}
}
I checked it with some debug statement, the file will be loaded and the function handle will be called.
But the uncaught Exception is still on my page.
What is wrong?
try using a string URL instead of an array for this kind of scenario.
I remember having a similar problem due to the fact, that the router has not been setup at that point and therefore not being able to convert the URL array to the corresponding array