dotkernel / dot-auth-social
Authentication wrapper for social providers
1.3.1
2025-01-27 12:42 UTC
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
- laminas/laminas-servicemanager: ^3.22 || ^4.0
- league/oauth2-client: ^2.6
- league/oauth2-facebook: ^2.2
Requires (Dev)
- laminas/laminas-coding-standard: ^3.0
- phpunit/phpunit: ^10.5
- vimeo/psalm: ^6.0
README
dot-auth-social is a wrapper on top of thephpleague/oauth2-client social providers.
dot-auth-social badges
Requirements
- PHP: 8.1, 8.2, 8.3 or 8.4
Installation
Run the following command in your project directory:
composer require dotkernel/dot-auth-social
After installing, add the ConfigProvider
class to your configuration aggregate.
Create a new file social-authentication.global.php
in config/autoload
with the following contents:
return [ 'social_authentication' => [ 'facebook' => [ 'client_id' => '', 'client_secret' => '', 'redirect_uri' => '', 'graph_api_version' => '', ] ] ];
Make sure to populate the array with your credentials.
Usage
In this example we will create a new controller, but you can use an existing one too.
<?php namespace Frontend\User\Controller; use Dot\Controller\AbstractActionController; use Dot\AuthSocial\Service\AuthenticationServiceInterface; use Laminas\Diactoros\Response\RedirectResponse; use Psr\Http\Message\ResponseInterface; class FacebookController extends AbstractActionController { private AuthenticationServiceInterface $service; public function __construct(AuthenticationServiceInterface $service) { $this->service = $service; } public function authAction(): ResponseInterface { $code = $this->request->getQueryParams()['code'] ?? false; if (! $code) { return new RedirectResponse($this->service->getAuthorizationUrl()); } $result = $this->service->authenticate($code); if (! $result->isValid()) { // invalid authentication, check $result->getMessages() for errors. } else { // valid authentication, use $result->getArrayCopy() to get the user details } } }
Create a factory for the controller:
<?php use Dot\AuthSocial\Service\FacebookService; use Psr\Container\ContainerInterface; class FacebookControllerFactory { public function __invoke(ContainerInterface $container): FacebookController { return new FacebookController($container->get(FacebookService::class)); } }
Make sure to register your controller with the factory in ConfigProvider
.