2016-10-07 27 views
1

我使用signInWithCustomToken,身份驗證後,我找不到在哪裏存儲我的自定義聲明,我在服務器端(createCustomToken)已設置數據。哪裏可以找到auth.token數據,裏面火力對象

我可以通過auth.token在firebase規則中看到它們,但是如何通過我的javascript代碼中的firebase對象訪問它們。

+0

你有試過嗎? 'var user = firebase.auth()。currentUser;'https://firebase.google.com/docs/auth/web/manage-users –

+0

是的,我試過了,但無法在currentUser中找到我的自定義屬性。 –

回答

3

令牌中的信息不會自動提供給您的應用程序代碼。但它被嵌入的道理,所以你可以自己對其進行解碼:

function parseJwt (token) { 
    var base64Url = token.split('.')[1]; 
    var base64 = base64Url.replace('-', '+').replace('_', '/'); 
    return JSON.parse(window.atob(base64)); 
}; 

var user = firebase.auth().currentUser 
user.getToken().then(data => { 
    console.log(parseJwt(data)); 
}); 

的函數來解析JWT來自這個問題:How to decode jwt token in javascript

你會注意到,它並沒有驗證ID令牌有效。這在客戶端代碼中似乎很好,因爲無論如何,這些信息都會被用戶自己使用。但是,如果您確實想驗證令牌,則必須使用更復雜的方法。

+0

只是出於好奇,爲什麼不包含在用戶對象信息,對我來說會更有意義後'signInWithCustomToken'所有自定義字段將可我的用戶或AUTH()對象中。 –