无法使用$ auth-> getUser($ uid)获取用户表单kreait / firebase-php;

时间:2019-11-20 18:45:14

标签: php firebase firebase-authentication

我有我的PHP代码,

use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Firebase\Auth\Token\Exception\InvalidToken;
use Kreait\Firebase\Auth;


$serviceAccount = ServiceAccount::fromJsonFile(APP_ROOT_MAIN.'/path/to/service/ac.json'); 

$factory = (new Factory)
->withServiceAccount($serviceAccount);

$auth = $factory->createAuth();

try {
    $verifiedIdToken = $auth->verifyIdToken($token);
} catch (Exception $e) {
    echo "Failed"
}

$uid = $verifiedIdToken->getClaim('sub');
echo $uid;
$user = $auth->getUser($uid);
print_r($user);

借助上述代码,我可以获取用户的UID,但print_r($ user)的输出如下所示

    Kreait\Firebase\Auth\User Object
(
    [idToken:Kreait\Firebase\Auth\User:private] => Lcobucci\JWT\Token Object
        (
            [headers:Lcobucci\JWT\Token:private] => Array
                (
                    [alg] => RS256
                    [kid] => jhjkhk90pi9po;jmklkl
                    [typ] => JWT
                )

            [claims:Lcobucci\JWT\Token:private] => Array
                (
                    [name] => Lcobucci\JWT\Claim\Basic Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => name
                            [value:Lcobucci\JWT\Claim\Basic:private] => Sachin Kekarjawalekar
                        )

                    [picture] => Lcobucci\JWT\Claim\Basic Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => picture
                            [value:Lcobucci\JWT\Claim\Basic:private] => https://lh3.googleusercontent.com/a-/AAuE7mC3to6_ClEkU
                        )

                    [iss] => Lcobucci\JWT\Claim\EqualsTo Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => iss
                            [value:Lcobucci\JWT\Claim\Basic:private] => https://securetoken.google.com/-sachin
                        )

                    [aud] => Lcobucci\JWT\Claim\EqualsTo Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => aud
                            [value:Lcobucci\JWT\Claim\Basic:private] => sachin
                        )

                    [auth_time] => Lcobucci\JWT\Claim\Basic Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => auth_time
                            [value:Lcobucci\JWT\Claim\Basic:private] => 1574274974
                        )

                    [user_id] => Lcobucci\JWT\Claim\Basic Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => user_id
                            [value:Lcobucci\JWT\Claim\Basic:private] => some_user_ud
                        )

                    [sub] => Lcobucci\JWT\Claim\EqualsTo Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => sub
                            [value:Lcobucci\JWT\Claim\Basic:private] => some_user_ud
                        )

                    [iat] => Lcobucci\JWT\Claim\LesserOrEqualsTo Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => iat
                            [value:Lcobucci\JWT\Claim\Basic:private] => 1574274974
                        )

                    [exp] => Lcobucci\JWT\Claim\GreaterOrEqualsTo Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => exp
                            [value:Lcobucci\JWT\Claim\Basic:private] => 1574278574
                        )

                    [email] => Lcobucci\JWT\Claim\Basic Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => email
                            [value:Lcobucci\JWT\Claim\Basic:private] => someemail@gmail.com
                        )

                    [email_verified] => Lcobucci\JWT\Claim\Basic Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => email_verified
                            [value:Lcobucci\JWT\Claim\Basic:private] => 1
                        )

                    [firebase] => Lcobucci\JWT\Claim\Basic Object
                        (
                            [name:Lcobucci\JWT\Claim\Basic:private] => firebase
                            [value:Lcobucci\JWT\Claim\Basic:private] => stdClass Object
                                (
                                    [identities] => stdClass Object
                                        (
                                            [email] => Array
                                                (
                                                    [0] =>someemail@email.com
                                                )

                                        )

                                    [sign_in_provider] => custom
                                )

                        )

                )

我如何以这种格式打印$ user?

{
"uid": "jEazVdPDhqec0tnEOG7vM5wbDyU2",
"email": "user@domain.tld",
"emailVerified": true,
"displayName": null,
"photoUrl": null,
"phoneNumber": null,
"disabled": false,
"metadata": {
    "createdAt": "2018-02-14T15:41:32+00:00",
    "lastLoginAt": "2018-02-14T15:41:32+00:00"
},
"providerData": [
    {
        "uid": "user@domain.tld",
        "displayName": null,
        "email": "user@domain.tld",
        "photoUrl": null,
        "providerId": "password",
        "phoneNumber": null
    }
],
"passwordHash": "UkVEQUNURUQ=",


 "customClaims": null,
    "tokensValidAfterTime": "2018-02-14T15:41:32+00:00"
}

我需要使用功能$ auth-> getUser('some-uid');获取用户的电子邮件ID和其他详细信息。但是我无法以上述格式获得预期的输出,但正在获取该类的对象作为输出。 请参阅:https://firebase-php.readthedocs.io/en/stable/user-management.html#user-records

1 个答案:

答案 0 :(得分:0)

您可以使用json_encode()获得输出:

$output = json_encode($user, \JSON_PRETTY_PRINT);

如果要访问用户记录的各个属性,则可以直接使用UserRecord对象并进行访问(请参阅https://github.com/kreait/firebase-php/blob/master/src/Firebase/Auth/UserRecord.php

请确保使用最新版本的SDK-Kreait\Auth\User显示的print_r()对象在当前维护的4.x版本中不存在,应该返回的是UserRecord个对象。