clarity-project/yandex-oauth-bundle
Installation Add the bundle via Composer:
composer require clarity-project/yandex-oauth-bundle
Enable it in config/bundles.php:
return [
// ...
Clarity\YandexOAuthBundle\ClarityYandexOAuthBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console clarity:yandex-oauth:install
Update config/packages/clarity_yandex_oauth.yaml with your Yandex OAuth credentials:
clarity_yandex_oauth:
client_id: 'your_client_id'
client_secret: 'your_client_secret'
redirect_uri: 'https://your-app.com/connect/yandex/check'
First Use Case: Authentication Flow
# config/routes.yaml
yandex_oauth_connect:
path: /connect/yandex
controller: Clarity\YandexOAuthBundle\Controller\AuthController::connectAction
// In your controller
return $this->redirect($this->generateUrl('yandex_oauth_connect'));
Fetching Tokens Use the service to retrieve or refresh tokens:
$tokenManager = $this->get('clarity_yandex_oauth.token_manager');
$token = $tokenManager->getAccessToken(); // Auto-refreshes if expired
Storing Tokens in Doctrine
The bundle provides an entity (Clarity\YandexOAuthBundle\Entity\Token) for persistence. Extend or use it directly:
$tokenEntity = new Token();
$tokenEntity->setAccessToken($token->getAccessToken());
$tokenEntity->setRefreshToken($token->getRefreshToken());
$em->persist($tokenEntity);
$em->flush();
API Integration
Use the YandexApiClient service to interact with Yandex APIs:
$client = $this->get('clarity_yandex_oauth.api_client');
$response = $client->get('/v1/user/info'); // Example endpoint
$data = $response->getData(); // Serialized via JMSSerializer
TokenRefreshedEvent).jm_serializer in config/packages/clarity_yandex_oauth.yaml.tenant_id) in the Token entity for shared environments.Redirect URI Mismatch
redirect_uri. Ensure it matches exactly (including https:// vs. http://).clarity_yandex_oauth.yaml and Yandex Developer Console settings.Token Expiry Handling
config/packages/misd_guzzle.yaml:
misd_guzzle:
plugins:
- Clarity\YandexOAuthBundle\Plugin\LoggingPlugin
Doctrine Entity Conflicts
Token entity, namespace collisions may occur.config/packages/doctrine.yaml:
doctrine:
orm:
entity_managers:
default:
mappings:
ClarityYandexOAuthBundle: ~
Testing OAuth Flows Use Yandex’s sandbox mode for local development:
clarity_yandex_oauth:
sandbox: true
Custom API Responses
Extend the YandexApiResponse class to handle Yandex-specific data formats:
// src/Service/YandexCustomResponse.php
class YandexCustomResponse extends YandexApiResponse
{
public function getUserId()
{
return $this->data['id'] ?? null;
}
}
Register it in services.yaml:
services:
clarity_yandex_oauth.api_response:
class: App\Service\YandexCustomResponse
tags: ['clarity_yandex_oauth.api_response']
Logging Enable debug logs for token operations:
clarity_yandex_oauth:
debug: true
Performance Cache token refresh operations if using high-frequency APIs:
$token = $tokenManager->getAccessToken(['cache_key' => 'yandex_token']);
How can I help you explore Laravel packages today?