I have my PHP code which looks like,
use KreaitFirebaseFactory;
use KreaitFirebaseServiceAccount;
use FirebaseAuthTokenExceptionInvalidToken;
use KreaitFirebaseAuth;
$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);
With the help of the above code, I am able to get the UID of the user but the output for the print_r($user) looks like,
KreaitFirebaseAuthUser Object
(
[idToken:KreaitFirebaseAuthUser:private] => LcobucciJWTToken Object
(
[headers:LcobucciJWTToken:private] => Array
(
[alg] => RS256
[kid] => jhjkhk90pi9po;jmklkl
[typ] => JWT
)
[claims:LcobucciJWTToken:private] => Array
(
[name] => LcobucciJWTClaimBasic Object
(
[name:LcobucciJWTClaimBasic:private] => name
[value:LcobucciJWTClaimBasic:private] => Sachin Kekarjawalekar
)
[picture] => LcobucciJWTClaimBasic Object
(
[name:LcobucciJWTClaimBasic:private] => picture
[value:LcobucciJWTClaimBasic:private] => https://lh3.googleusercontent.com/a-/AAuE7mC3to6_ClEkU
)
[iss] => LcobucciJWTClaimEqualsTo Object
(
[name:LcobucciJWTClaimBasic:private] => iss
[value:LcobucciJWTClaimBasic:private] => https://securetoken.google.com/-sachin
)
[aud] => LcobucciJWTClaimEqualsTo Object
(
[name:LcobucciJWTClaimBasic:private] => aud
[value:LcobucciJWTClaimBasic:private] => sachin
)
[auth_time] => LcobucciJWTClaimBasic Object
(
[name:LcobucciJWTClaimBasic:private] => auth_time
[value:LcobucciJWTClaimBasic:private] => 1574274974
)
[user_id] => LcobucciJWTClaimBasic Object
(
[name:LcobucciJWTClaimBasic:private] => user_id
[value:LcobucciJWTClaimBasic:private] => some_user_ud
)
[sub] => LcobucciJWTClaimEqualsTo Object
(
[name:LcobucciJWTClaimBasic:private] => sub
[value:LcobucciJWTClaimBasic:private] => some_user_ud
)
[iat] => LcobucciJWTClaimLesserOrEqualsTo Object
(
[name:LcobucciJWTClaimBasic:private] => iat
[value:LcobucciJWTClaimBasic:private] => 1574274974
)
[exp] => LcobucciJWTClaimGreaterOrEqualsTo Object
(
[name:LcobucciJWTClaimBasic:private] => exp
[value:LcobucciJWTClaimBasic:private] => 1574278574
)
[email] => LcobucciJWTClaimBasic Object
(
[name:LcobucciJWTClaimBasic:private] => email
[value:LcobucciJWTClaimBasic:private] => someemail@gmail.com
)
[email_verified] => LcobucciJWTClaimBasic Object
(
[name:LcobucciJWTClaimBasic:private] => email_verified
[value:LcobucciJWTClaimBasic:private] => 1
)
[firebase] => LcobucciJWTClaimBasic Object
(
[name:LcobucciJWTClaimBasic:private] => firebase
[value:LcobucciJWTClaimBasic:private] => stdClass Object
(
[identities] => stdClass Object
(
[email] => Array
(
[0] =>someemail@email.com
)
)
[sign_in_provider] => custom
)
)
)
How can I get print the $user in this format?
{
"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"
}
I need to get the email Id and other details of the user from using the function $auth->getUser(‘some-uid’); but I am unable to get the expected output with the above type of format, I am getting an object of the class as an output. Please refer: https://firebase-php.readthedocs.io/en/stable/user-management.html#user-records
Advertisement
Answer
you can get the output by using json_encode()
:
$output = json_encode($user, JSON_PRETTY_PRINT);
If you want to access the individual properties of the user record, you can use the UserRecord
object directly and access (see https://github.com/kreait/firebase-php/blob/master/src/Firebase/Auth/UserRecord.php)
Please make sure to use the latest release of the SDK – the KreaitAuthUser
object that your print_r()
shows, doesn’t exist in the currently maintained 4.x releases, what should be returned is a UserRecord
object.