简体   繁体   中英

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

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 ?

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));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM