2017-04-10 75 views
0

如果我擁有帶有某些聲明的自定義令牌,並且使用它登錄Firebase,是否有任何方式使用Web SDK從應用內部訪問這些聲明?從Web SDK訪問Firebase自定義令牌聲明

例如,如果我的自定義標記是這樣

{ 
:iss => $service_account_email, 
:sub => $service_account_email, 
:aud => "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit", 
:iat => now_seconds, 
:exp => now_seconds+(60*60), # Maximum expiration time is one hour 
:uid => uid, 
:claims => {:premium_account => is_premium_account} 
} 

我想知道是否有類似的信息(來自內部應用程序):

firebase.auth.token.claims.premium_account 

我沒有找到文檔中的任何此類內容。

+1

http://stackoverflow.com/questions/39917020/where-to-find-auth-token-data-inside-firebase-objects – bojeil

+0

謝謝@bojeil。所以它看起來不能從sdk訪問... – Bruno

回答

0

claims嵌入在令牌中。

這裏是一個例子代碼的Web客戶端上使用jwt-decode提取從令牌的權利要求:

import jwt_decode from './jwt-decode'; 

firebase.auth().currentUser.getToken().then((token) => { 
    console.log(token); 
    console.log(jwt_decode(token)); 
}); 
0

這裏是在其上的文檔:https://firebase.google.com/docs/auth/admin/custom-claims

我認爲它的要點,是一旦您通過後端代碼(admin sdk或firebase函數)向用戶附加了自定義聲明,您可以base64解碼當前用戶令牌。 該文檔參考了JavaScript base64解碼的mozilla文章:https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

您可以JSON.parse解碼的令牌,您的自定義聲明將顯示在那裏。該文檔相當不錯。