diff --git a/src/Firebase/Auth.php b/src/Firebase/Auth.php index 65a1e68b..d079c22f 100644 --- a/src/Firebase/Auth.php +++ b/src/Firebase/Auth.php @@ -59,7 +59,7 @@ /** * @internal */ -final class Auth implements Contract\Auth +final class Auth implements Contract\Auth, Contract\Transitional\FederatedUserFetcher { private readonly Parser $jwtParser; @@ -210,6 +210,22 @@ public function getUserByPhoneNumber(Stringable|string $phoneNumber): UserRecord return UserRecord::fromResponseData($data['users'][0]); } + public function getUserByProviderUid(Stringable|string $providerId, Stringable|string $providerUid): UserRecord + { + $providerId = (string) $providerId; + $providerUid = (string) $providerUid; + + $response = $this->client->getUserByProviderUid($providerId, $providerUid); + + $data = Json::decode((string) $response->getBody(), true); + + if (empty($data['users'][0])) { + throw new UserNotFound("No user with federated account ID '{$providerId}:{$providerUid}' found."); + } + + return UserRecord::fromResponseData($data['users'][0]); + } + public function createAnonymousUser(): UserRecord { return $this->createUser(CreateUser::new()); diff --git a/src/Firebase/Auth/ApiClient.php b/src/Firebase/Auth/ApiClient.php index 7775c615..749a53bd 100644 --- a/src/Firebase/Auth/ApiClient.php +++ b/src/Firebase/Auth/ApiClient.php @@ -118,6 +118,16 @@ public function getUserByPhoneNumber(string $phoneNumber): ResponseInterface return $this->requestApi($url, ['phoneNumber' => [$phoneNumber]]); } + /** + * @throws AuthException + */ + public function getUserByProviderUid(string $providerId, string $uid): ResponseInterface + { + $url = $this->awareAuthResourceUrlBuilder->getUrl('/accounts:lookup'); + + return $this->requestApi($url, ['federatedUserId' => [['providerId' => $providerId, 'rawId' => $uid]]]); + } + /** * @throws AuthException */ diff --git a/src/Firebase/Contract/Transitional/FederatedUserFetcher.php b/src/Firebase/Contract/Transitional/FederatedUserFetcher.php new file mode 100644 index 00000000..c894ddb0 --- /dev/null +++ b/src/Firebase/Contract/Transitional/FederatedUserFetcher.php @@ -0,0 +1,24 @@ +