首页 > 解决方案 > Firebase 在 php 上创建自定义令牌始终在 php 上返回 null

问题描述

嗨,我正在使用 cakephp 作为后端,并希望为我的网站生成一个自定义令牌,该令牌将在我的客户端使用。下面是我生成自定义代码的代码

   $activatedOfferwall = $session->read('ActiveOfferwall'); // 0209341c-da14-46b9-b3c9-8410472e13d2
    $factory = (new Factory)->withServiceAccount(\App\Model\Constants\FirebaseConstants::JSON_FILE)->withDatabaseUri(\App\Model\Constants\FirebaseConstants::DEFAULT_URL);
    $auth = $factory->createAuth();
    $customToken = $auth->createCustomToken($activatedOfferwall['UserHash']);
    Log::debug(json_encode($customToken));

火基地规则是

在此处输入图像描述

但这总是返回一个空的 json。我在这里想念什么?

标签: phpfirebase

解决方案


对于正在寻找的人,我找到了解决方案。规则必须是 在此处输入图像描述

$generator = CustomTokenGenerator::withClientEmailAndPrivateKey(
            $email,
            $privateKey
        );
        $token = $generator->createCustomToken('anthony@gmail.com');

然后在客户端发送令牌

firebase.auth().signInWithCustomToken(usertoken)
            .then((userCredential) => {
                // Signed in
                var user = userCredential.user;
                firebase.auth().currentUser.getIdToken( /* forceRefresh */ true).then(function(idToken) {
                    console.log(idToken)
                }).catch(function(error) {
                    // Handle error
                });
            })
            .catch((error) => {
                var errorCode = error.code;
                var errorMessage = error.message;
                console.log(error);
                // ...
            });

推荐阅读