1. Go to this page and download the library: Download demroos/oauth2-alvario library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
demroos / oauth2-alvario example snippets
namespace App\Controller;
use Alvario\OAuth2\AlvarioUser;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AlvarioController extends AbstractController
{
/**
* @Route("/connect/alvario", name="connect_alvario_start")
* @param ClientRegistry $clientRegistry
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function connectAction(ClientRegistry $clientRegistry)
{
$response = $clientRegistry
->getClient('alvario')
->redirect([
'public_profile'
]);
return $response;
}
/**
* @Route("/connect/alvario/check", name="connect_alvario_check")
* @param Request $request
* @param ClientRegistry $clientRegistry
* @return Response
* @throws \Exception
*/
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
{
return Response::create('OK');
}
}
namespace App\Security;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
class AlvarioAuthenticator extends SocialAuthenticator
{
/** @var ClientRegistry */
private $clientRegistry;
/** @var EntityManagerInterface */
private $em;
public function __construct(ClientRegistry $clientRegistry, EntityManagerInterface $em)
{
$this->clientRegistry = $clientRegistry;
$this->em = $em;
}
public function start(Request $request, AuthenticationException $authException = null)
{
return new RedirectResponse(
'/connect/alvario', // might be the site, where users choose their oauth provider
Response::HTTP_TEMPORARY_REDIRECT
);
}
public function supports(Request $request)
{
return $request->attributes->get('_route') === 'connect_alvario_check';
}
public function getCredentials(Request $request)
{
return $this->fetchAccessToken($this->getAlvarioClient());
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
/** @var AlvarioUser $alvarioUser */
$alvarioUser = $this->getAlvarioClient()
->fetchUserFromToken($credentials);
$user = $this->em->getRepository(User::class)
->findByOauthId($alvarioUser->getId());
if ($user instanceof UserInterface) {
return $user;
}
// create new local user
$user = new User();
$user->setOauthId($alvarioUser->getId());
$user->setFirstName($alvarioUser->getFirstName());
$user->setLastName($alvarioUser->getLastName());
$this->em->persist($user);
$this->em->flush();
return $user;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$message = strtr($exception->getMessageKey(), $exception->getMessageData());
return new Response($message, Response::HTTP_FORBIDDEN);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
// on success, let the request continue
// or redirect to other page
return null;
}
private function getAlvarioClient()
{
return $this->clientRegistry
->getClient('alvario');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.