Носитель аутентификации в React - PullRequest
0 голосов
/ 27 ноября 2018

нужна помощь.Как я могу использовать Athentication Bearer с суперагентом в React.Я не уверен в синтаксисе и не могу найти пример.

Что я делаю сейчас

   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 ]

0 голосов
/ 27 ноября 2018

Способ установки заголовков заключается в предоставлении имени и значения элемента заголовка, попробуйте:

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 параметра (имя, значение).

0 голосов
/ 27 ноября 2018

Попробуйте использовать .auth('Bearer', this.state.id_token) http://visionmedia.github.io/superagent/#authentication

...