Final Class Yiisoft\Yii\AuthClient\AuthAction
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
AuthAction is a PSR-15 middleware, which performs authentication via {@see OAuth2} auth clients (including {@see OpenIdConnect}).
Usage, registered as a DI definition and attached to a route:
// config/di.php
AuthAction::class => static fn (
Collection $clientCollection,
Aliases $aliases,
WebView $view,
ResponseFactoryInterface $responseFactory,
CurrentRoute $currentRoute,
) => (new AuthAction($clientCollection, $aliases, $view, $responseFactory, $currentRoute))
->withSuccessUrl('/site/index')
->withCancelUrl('/site/login')
->withSuccessCallback(function (AuthClientInterface $client) {
$attributes = $client->getUserAttributes();
// user login or signup comes here
})
->withCancelCallback(function (AuthClientInterface $client) {
// set flash, logging, etc.
}),
// config/routes.php
Route::methods(['GET', 'POST'], '/auth/{authclient}')->action(AuthAction::class),
Usually authentication via external services is performed inside the popup window. This action handles the redirection and closing of popup window correctly.
The matched {authclient} route placeholder is read from {@see \CurrentRoute.
See also:
Public Methods
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| AUTH_NAME | 'auth_displayname' | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $clientCollection | Yiisoft\ |
|
| $aliases | \ |
|
| $view | \ |
|
| $responseFactory | \ |
|
| $currentRoute | \ |
|
public function __construct(
/**
* @var Collection
* It should point to {@see Collection} instance.
*/
private readonly Collection $clientCollection,
private readonly Aliases $aliases,
private readonly WebView $view,
private readonly ResponseFactoryInterface $responseFactory,
private readonly CurrentRoute $currentRoute,
) {}
| public \ | ||
| $request | \ |
|
| $handler | \ |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$clientId = (string) $this->currentRoute->getArgument($this->clientIdGetParamName);
if (strlen($clientId) > 0) {
if (!$this->clientCollection->hasClient($clientId)) {
return $this->responseFactory->createResponse(Status::NOT_FOUND, "Unknown auth client '{$clientId}'");
}
$client = $this->clientCollection->getClient($clientId);
return $this->auth($client, $request);
}
return $this->responseFactory->createResponse(Status::NOT_FOUND);
}
| public Yiisoft\ | ||
| $callback | callable |
PHP callback, which should be triggered in case of authentication cancellation.
This callback should accept {@see \ |
public function withCancelCallback(callable $callback): self
{
$new = clone $this;
$new->cancelCallback = $callback;
return $new;
}
| public Yiisoft\ | ||
| $url | string |
Cancel URL. |
public function withCancelUrl(string $url): self
{
$new = clone $this;
$new->cancelUrl = $url;
return $new;
}
| public Yiisoft\ | ||
| $callback | callable |
PHP callback, which should be triggered in case of successful authentication.
This callback should accept {@see \ |
public function withSuccessCallback(callable $callback): self
{
$new = clone $this;
$new->successCallback = $callback;
return $new;
}
| public Yiisoft\ | ||
| $url | string |
Successful URL. |
public function withSuccessUrl(string $url): self
{
$new = clone $this;
$new->successUrl = $url;
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.