Creating a Custom Authentication
Authentication classes are simple to create. They must implement the AuthInterface
interface and implement
the withAuth()
method. This method takes in an instance of a Psr RequestInterface generated by the ApiClient
.
Authentication
classes simply return an augmented request object for the ApiClient
to send over the wire.
Below is an example of how to create an authentication class that adds a JWT to the request:
use WPRestClient\Core\AuthInterface;
use Psr\Http\Message\RequestInterface;
class JwtAuthentication implements AuthInterface
{
protected string $jwt;
public function __construct(string $jwt) {
$this->jwt = $jwt;
}
public function withAuth(RequestInterface $request): RequestInterface
{
return $request->withAddedHeader('Authorization', 'Bearer ' . $this->jwt);
}
}