Authentication anonymous user

I am trying to learn authentication (2.0 with cakephp 4). I find the cookbook instructions a bit short and am left with many questions.

I have baked policies to start with:
public function canView(IdentityInterface $user, Originator $originator)
return true;

I get the following error message, when no user is logged in:
“# Argument 1 passed to App\Policy\OriginatorPolicy::canView() must be an instance of Authorization\IdentityInterface, null given”

What do I need to do to handle anonymous users? I have read that I get “null” for $user, that is ok. But what do I need to do to prevent this error message?

public function canView(IdentityInterface $user = null, Originator $originator)

might work. It indicates that the $user can be null. You’d need to handle that scenario in your function, of course.

1 Like