簡體   English   中英

使用 Firebase-PHP 驗證 Firebase ID 令牌

[英]Verify Firebase ID Token with Firebase-PHP

我正在使用 Firebase-Auth 在我用 PHP 編碼的網絡應用上授權用戶。 授權本身是用 Javascript 完成的,它在 Ajax 請求上執行以驗證用戶是否已登錄。

為了在服務器上使用 Firebase-Admin,我已經實現了Firebase-PHP

在每個AJX請求我現在就登錄的用戶ID和ID令牌,這是我想在PHP驗證像它被寫在這里的文檔。

驗證本身工作正常。 如果令牌存在,我會得到一個“IDToken”-Object。 但是如何再次從該對象中獲取用戶 ID 以驗證令牌是否適合該用戶?

$idTokenString = '...';

try {
    $verifiedIdToken = $firebase->getAuth()->verifyIdToken($idTokenString);
    // This is the Token-Object where I cant't find the uid-get-Function
} catch (InvalidIdToken $e) {
    echo $e->getMessage();
}

我在文檔或我搜索的類中找不到方法。

圖書館的維護者在這里 - 文檔確實缺少該信息,但現在包含在內並且整個頁面已經過大修:

https://firebase-php.readthedocs.io/en/latest/authentication.html#verify-a-firebase-id-token

您可以從 ID 令牌的sub聲明中檢索 UID:

try {
    $verifiedIdToken = $firebase->getAuth()->verifyIdToken($idToken);
} catch (InvalidToken $e) {
    echo $e->getMessage();
}
    
$uid = $verifiedIdToken->getClaim('sub'); // lcobucci/jwt:^3.0
// or 
$uid = $verifiedIdToken->claims()->get('sub'); // lcobucci/jwt:^4.0

$user = $firebase->getAuth()->getUser($uid);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM