<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpClient\HttpClient;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
// if ($this->getUser()) {
// return $this->redirectToRoute('target_path');
// }
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
dump($error,$authenticationUtils);
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/connect/microsoft", name="connect_microsoft")
*/
public function connect(ClientRegistry $clientRegistry)
{
// Abrufen des Microsoft-Clients
$client = $clientRegistry->getClient('microsoft');
// Leitet den Benutzer zu Microsoft für die Authentifizierung weiter
return $client->redirect(['User.read'], []);
}
/**
* @Route("/connect/microsoft/check", name="connect_microsoft_check")
*/
public function check(Request $request, ClientRegistry $clientRegistry)
{
$client = $clientRegistry->getClient('microsoft');
$user = $client->fetchUser();
dump($user);
// Benutzerdaten verwenden
$email = $user->getEmail();
$firstName = $user->getFirstName();
$lastName = $user->getLastName();
// Beispiel: Ausgabe der Benutzerdaten
return $this->json([
'email' => $email,
'first_name' => $firstName,
'last_name' => $lastName,
]);
}
/**
* @Route("/logout", name="app_logout")
*/
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}