-
-
Save LevPrav999/02149874b4bd496e6423092e03d5e590 to your computer and use it in GitHub Desktop.
discord oauth2 symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class DiscordAuthenticatorService extends OAuth2Authenticator implements AuthenticationEntryPointInterface | |
{ | |
public function __construct( | |
private readonly ClientRegistry $clientRegistry, | |
private readonly EntityManagerInterface $em, | |
private readonly RouterInterface $router, | |
private readonly UserRepositoryInterface $userRepository | |
) {} | |
public function start(Request $request, AuthenticationException $authException = null): RedirectResponse | |
{ | |
return new RedirectResponse($this->router->generate("auth_discord_start"), Response::HTTP_TEMPORARY_REDIRECT); | |
} | |
public function supports(Request $request): ?bool | |
{ | |
return $request->attributes->get("_route") === "auth_discord_login"; | |
} | |
public function authenticate(Request $request): SelfValidatingPassport | |
{ | |
$client = $this->clientRegistry->getClient("discord"); | |
$accessToken = $this->fetchAccessToken($client); | |
return new SelfValidatingPassport( | |
new UserBadge($accessToken->getToken(), function () use ($accessToken, $client) { | |
/** @var DiscordResourceOwner $discordUser */ | |
$discordUser = $client->fetchUserFromToken($accessToken); | |
$user = $this->userRepository->findOneBy(["discordId" => $discordUser->getId()]); | |
if (null === $user) { | |
$user = new User($discordUser->getId()); | |
$this->em->persist($user); | |
} | |
$this->em->flush(); | |
return $user; | |
}) | |
); | |
} | |
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | |
{ | |
return null; | |
} | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | |
{ | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[AsController] | |
#[Route("/auth/discord", name: "auth_discord_")] | |
final class LoginController | |
{ | |
/** | |
* @throws IdentityProviderException | |
*/ | |
#[Route("/login", name: "login")] | |
public function login(#[MapQueryParameter] ?string $code, ClientRegistry $clientRegistry): JsonResponse | |
{ | |
$token = $clientRegistry->getClient('discord')->getAccessToken([ | |
'code' => $code | |
])->getToken(); | |
$user = $clientRegistry->getClient('discord')->getOAuth2Provider()->getResourceOwner($token); | |
return new JsonResponse(["ok" => $user]); | |
} | |
#[Route("/start", name: "start")] | |
public function start(ClientRegistry $clientRegistry): RedirectResponse | |
{ | |
return $clientRegistry->getClient("discord")->redirect(["identify"]); | |
} | |
/** | |
* @throws \Exception | |
*/ | |
#[Route('/logout', name: 'app_logout')] | |
public function logout() | |
{ | |
throw new \Exception('Logging out'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment