我正在react native上构建一个前端应用程序。一个文件(屏幕)正在使用以下代码生成令牌
fetch("http://192.168.1.11:8080/api/login", {
method: 'POST',
body: JSON.stringify({
username: 'user'
}),
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}).then((response) => response.json())
.then((json) => {
this.setState({ JwToken: json.token });
}).then((json) =>this.props.navigation.navigate('Home')/)
.catch((error) => console.error(error))
.finally(() => {
this.setState({ isLoading: false });
});
在另一个文件中,我必须使用该令牌进行验证
fetch('http://192.168.1.11:8080/api/books', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + JwToken
},
body: JSON.stringify({
title: 'new book',
price: 4
})
});
我不知道如何在两个文件之间传递令牌。
1条答案
按热度按时间nzrxty8p1#
这是我以前从react导航文档中引用过的,似乎符合您的需要。