javascript - Authentication in a React Native WebView - Stack Overflow

I have a working react-native application, which I would like to extend in a flexible way by adding a t

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.

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 munity ponent react-native-webview. So I cannot set an authorisation token on a request.

This plicates 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?

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.

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 munity ponent react-native-webview. So I cannot set an authorisation token on a request.

This plicates 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?

Share Improve this question asked Feb 26, 2019 at 12:28 dentemmdentemm 6,3894 gold badges33 silver badges44 bronze badges 3
  • 1 I wrote about it on my blog: smakosh./webview-native-authentication – Smakosh Commented Dec 10, 2019 at 9:54
  • @Smakosh your tutorial has deprecated dependencies. can it be updated to CY 2022? – mobibob Commented Aug 25, 2022 at 22:46
  • Will do when I'm free – Smakosh Commented Aug 31, 2022 at 10:18
Add a ment  | 

1 Answer 1

Reset to default 3

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./profile/alice', {
  headers: {
    'Authorization': /* your token here... */
  }
}).then(function (response) {
  return response.json();
}).then(function (myJson) {
  console.log(JSON.stringify(myJson));
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745140191a4613393.html

相关推荐

  • javascript - Authentication in a React Native WebView - Stack Overflow

    I have a working react-native application, which I would like to extend in a flexible way by adding a t

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信