2016-05-29 61 views
0

我正在使用Auth0的react-native-lock小部件進行用戶驗證,並且在成功登錄後,我將存儲令牌與AsyncStorage。在React Native中獲取來自Auth0應用程序的userinfo

如果用戶回到應用程序,我希望能夠跳過登錄並簡單地從Auth0獲取當前用戶信息。這似乎從該Auth0 API docs非常容易的,但我在我的應用程序取回消息「未經授權」:

async getUserinfo() { 
console.log('getting user info in getUserInfo()'); 
try { 
    let response = await fetch('https://xxxxx.auth0.com/userinfo', { 
    method: 'GET', 
    headers: { 
     Authorization: 'Bearer ${this.state.token.accessToken}', 
    }, 
    }); 
    let responseJson = await response.json(); 
    if(responseJson !== null) { 
    console.log('Got user info: ' + responseJson.email); 
    this.setState({ component: Temp, isLoading: false, profile: responseJson}); 
    } 
} catch (error) { 
    console.log('Error in retrieving userinfo from Auth0: ' + error.message); 
    this.setState({ component: Login, isLoading: false}); 
} 
} 

我缺少什麼?我找不到很多使用Auth0提取的例子,有沒有更好的方法可以使用?

回答

0

an associated GitHub issue範圍內發現的問題。如果有人在這裏,而不是那裏的土地問題得到有效解決,問題是,你需要使用:

Authorization: 'Bearer ' + this.state.token.accessToken, 

原始代碼試圖將一個字符串字面'Bearer ${this.state.token.accessToken}'內訪問變量。

相關問題