src/Controller/SecurityController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpClient\HttpClient;
  10. class SecurityController extends AbstractController
  11. {
  12. /**
  13. * @Route("/login", name="app_login")
  14. */
  15. public function login(AuthenticationUtils $authenticationUtils): Response
  16. {
  17. // if ($this->getUser()) {
  18. // return $this->redirectToRoute('target_path');
  19. // }
  20. // get the login error if there is one
  21. $error = $authenticationUtils->getLastAuthenticationError();
  22. // last username entered by the user
  23. $lastUsername = $authenticationUtils->getLastUsername();
  24. dump($error,$authenticationUtils);
  25. return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
  26. }
  27. /**
  28. * @Route("/connect/microsoft", name="connect_microsoft")
  29. */
  30. public function connect(ClientRegistry $clientRegistry)
  31. {
  32. // Abrufen des Microsoft-Clients
  33. $client = $clientRegistry->getClient('microsoft');
  34. // Leitet den Benutzer zu Microsoft für die Authentifizierung weiter
  35. return $client->redirect(['User.read'], []);
  36. }
  37. /**
  38. * @Route("/connect/microsoft/check", name="connect_microsoft_check")
  39. */
  40. public function check(Request $request, ClientRegistry $clientRegistry)
  41. {
  42. $client = $clientRegistry->getClient('microsoft');
  43. $user = $client->fetchUser();
  44. dump($user);
  45. // Benutzerdaten verwenden
  46. $email = $user->getEmail();
  47. $firstName = $user->getFirstName();
  48. $lastName = $user->getLastName();
  49. // Beispiel: Ausgabe der Benutzerdaten
  50. return $this->json([
  51. 'email' => $email,
  52. 'first_name' => $firstName,
  53. 'last_name' => $lastName,
  54. ]);
  55. }
  56. /**
  57. * @Route("/logout", name="app_logout")
  58. */
  59. public function logout(): void
  60. {
  61. throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  62. }
  63. }