Skip to content

Find Firebase user(s) by federated user id #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 7.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/Firebase/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
/**
* @internal
*/
final class Auth implements Contract\Auth
final class Auth implements Contract\Auth, Contract\Transitional\FederatedUserFetcher
{
private readonly Parser $jwtParser;

Expand Down Expand Up @@ -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());
Expand Down
10 changes: 10 additions & 0 deletions src/Firebase/Auth/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Firebase/Contract/Transitional/FederatedUserFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Kreait\Firebase\Contract\Transitional;

use Kreait\Firebase\Auth\UserRecord;
use Kreait\Firebase\Exception;
use Stringable;

/**
* @TODO: This interface is intended to be integrated into the Auth interface on the next major release.
*/
interface FederatedUserFetcher
{
/**
* @param Stringable|non-empty-string $providerId
* @param Stringable|non-empty-string $providerUid
*
* @throws Exception\AuthException
* @throws Exception\FirebaseException
*/
public function getUserByProviderUid(Stringable|string $providerId, Stringable|string $providerUid): UserRecord;
}