oauth2-trackmania/examples/TrackMania.php
2023-02-16 11:00:25 +01:00

72 lines
2.2 KiB
PHP

<?php
namespace App\Controller\OAuth2;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
class TrackMania extends AbstractController
{
/**
* Link to this controller to start the "connect" process
*/
#[Route(
'/connect/trackmania',
name: 'connect_trackmania_start'
)]
public function connectAction(ClientRegistry $clientRegistry)
{
// will redirect to Ubisoft Connect OAuth2
return $clientRegistry
->getClient('TrackMania') // key used in config/packages/knpu_oauth2_client.yaml
->redirect([],[]);
}
/**
* After going to Ubisoft Connect OAuth2, you're redirected back here
* because this is the "redirect_route" you configured
* in config/packages/knpu_oauth2_client.yaml
*/
#[Route(
'/connect/trackmania/check',
name: 'connect_trackmania_check'
)]
public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
{
// ** if you want to *authenticate* the user, then
// leave this method blank and create a Guard authenticator
}
#[Route(
'/login',
name: 'app_login',
methods: ['GET', 'HEAD']
)]
public function login(Request $request, RequestStack $requestStack): RedirectResponse
{
$referer = $request->headers->get('referer');
if ($referer !== null) {
$session = $requestStack->getSession();
$session->set("PostLoginRedirect", $referer);
}
return $this->redirectToRoute('connect_trackmania_start');
}
/**
* @Route("/logout", name="app_logout", methods={"GET"})
*/
#[Route(
'/logout',
name: 'app_logout',
methods: ['GET', 'HEAD']
)]
public function logout(): void
{
// controller can be blank: it will never be called!
throw new \Exception('Don\'t forget to activate logout in security.yaml');
}
}