在React Native WebView中进行身份验证 [英] Authentication in a React Native WebView
问题描述
我有一个工作的本机应用程序,我想通过添加显示WebView
的选项卡灵活地扩展它.在这里,用户将能够看到其个人资料信息,因此我需要能够对每个用户进行身份验证.
I have a working react-native application, which I would like to extend in a flexible way by adding a tab which displays a WebView
. Here a user will be able to see their profile information, so I need to be able to authenticate each user.
我不认为这很难实现,因为用户已经在应用程序内进行了身份验证,但是显然不可能在react-native中向WebView添加自定义标头.社区组件react-native-webview也是如此.因此,我无法在请求上设置授权令牌.
I didn't think this would be difficult to implement since a user is already authenticated within the app, but apparently it is not possible to add custom headers to a WebView in react-native. And the same goes for the community component react-native-webview. So I cannot set an authorisation token on a request.
这使身份验证过程复杂化,因此我正在寻找在WebView中进行身份验证的另一种方法.令我惊讶的是,网络上没有太多有关如何执行此操作的信息.那么在React Native WebView
中解决auth的好方法是什么?
This complicates the process of authentication, so I am looking for an alternative approach to do authentication within a WebView. To my surprise there isn't much information available on the web on how to do this. So what is a good approach to tackle auth in a react native WebView
?
推荐答案
在WebView
中,您可以访问 fetch
.>
From within the WebView
you have access to the Fetch API in JavaScript which supports custom headers. So you can call fetch
.
fetch('https://example.com/profile/alice', {
headers: {
'Authorization': /* your token here... */
}
}).then(function (response) {
return response.json();
}).then(function (myJson) {
console.log(JSON.stringify(myJson));
});
这篇关于在React Native WebView中进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!