簡體   English   中英

React 中的承載身份驗證

[英]Bearer Authentication in React

如何在 React 中使用帶有 superagent 的承載身份驗證? 我不確定語法,也找不到示例。

我現在應該做什么

 showTransactionList = () => { superagent .post('http://193.124.114.46:3001/api/protected/transactions') .set({'Authorization': 'Bearer ' + this.state.id_token}) .accept('application/json') .then(res => { const posts = JSON.stringify(res.body); console.log(posts); }) .catch((err) => { console.log(err); throw err; }); }

謝謝!

設置標題的方式是提供標題項目名稱和值,請嘗試:

showTransactionList = () => {
    superagent
        .post('http://193.124.114.46:3001/api/protected/transactions')
        .set('Authorization', 'Bearer ' + this.state.id_token)
        .accept('application/json')
        .then(res => {
            const posts = JSON.stringify(res.body);
            console.log(posts);
        })
        .catch((err) => {
            console.log(err);
            throw err;                    
        });
}

因此,不要在標頭中設置對象,而是將其作為 2 個參數(名稱、值)傳遞。

而不是設置完整的標題

.set({'Authorization': 'Bearer ' + this.state.id_token})

您可以使用

.auth(this.state.id_token, { type: 'bearer' })

嘗試使用.auth('Bearer', this.state.id_token) http://visionmedia.github.io/superagent/#authentication

暫無
暫無

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

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