oauth2-trackmania/examples/TrackMania.php

73 lines
2.2 KiB
PHP
Raw Normal View History

2023-02-16 11:00:25 +01:00
<?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',
2023-02-16 15:25:53 +01:00
name: 'connect_trackmania_start',
priority: 10
2023-02-16 11:00:25 +01:00
)]
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',
2023-02-16 15:25:53 +01:00
name: 'connect_trackmania_check',
priority: 10
2023-02-16 11:00:25 +01:00
)]
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',
2023-02-16 15:25:53 +01:00
priority: 10,
2023-02-16 11:00:25 +01:00
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',
2023-02-16 15:25:53 +01:00
priority: 10,
2023-02-16 11:00:25 +01:00
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');
}
}