Skip to content

Instantly share code, notes, and snippets.

@kanzitelli
Last active June 27, 2023 08:30
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save kanzitelli/e5d198e96f81b7a8263745043766127c to your computer and use it in GitHub Desktop.
Save kanzitelli/e5d198e96f81b7a8263745043766127c to your computer and use it in GitHub Desktop.
React Native Trick: Get Cookies of WebView without using any native modules such as react-native-cookies. Might be helpful for getting JWT while making OAuth2 👽
// @flow
import React, { Component } from 'react';
import {
WebView,
} from 'react-native';
class LoginScreen extends Component {
state = {
cookies : {},
webViewUrl : ''
}
onNavigationStateChange = (webViewState: { url: string }) => {
const { url } = webViewState;
// when WebView.onMessage called, there is not-http(s) url
if(url.includes('http')) {
this.setState({ webViewUrl: url })
}
}
_checkNeededCookies = () => {
const { cookies, webViewUrl } = this.state;
if (webViewUrl === 'SUCCESS_URL') {
if (cookies['cookie-name-for-jwt']) {
alert(cookies['cookie-name-for-jwt']);
// do your magic...
}
}
}
_onMessage = (event) => {
const { data } = event.nativeEvent;
const cookies = data.split(';'); // `csrftoken=...; rur=...; mid=...; somethingelse=...`
cookies.forEach((cookie) => {
const c = cookie.trim().split('=');
const new_cookies = this.state.cookies;
new_cookies[c[0]] = c[1];
this.setState({ cookies: new_cookies });
});
this._checkNeededCookies();
}
render() {
const jsCode = "window.postMessage(document.cookie)"
// let jsCode = "window.postMessage(document.cookie= 'login=; expires=Bla bla bla')"; // if you need to write some cookies, not sure if it goes to shared cookies, most probably no :)
return (
<WebView
source={{ uri: 'AUTH_URL' }}
onNavigationStateChange={this.onNavigationStateChange}
onMessage={this._onMessage}
injectedJavaScript={jsCode}
style={{ flex: 1 }}
/>
);
}
}
export default LoginScreen;
@srinivasdamam
Copy link

srinivasdamam commented Sep 7, 2017

Hey this is not working, when the message is posted the webView stops loading the page, I have crossed checked everything but still this doesn't work.

@nick-castaneda
Copy link

The injection happens after page load. Maybe try programmatically changing the webview url. That worked for me.

@miguelnfuertesc
Copy link

Good trick! Thank you for sharing it.

@juanmicl
Copy link

Nice trick, but we can't read HttpsOnly cookies :(

@ivanferrer
Copy link

Same is security if use postMessage...

@ramisalem
Copy link

where did you pass the cookies to the URL ?

@imwilliamxy
Copy link

const jsCode = 'window.ReactNativeWebView.postMessage(document.cookie)';

New webview version use this line.

@anujsachan1990
Copy link

encountered one same issue, it doesn't return complete cookies
the output is something like below:
["okta-oauth-nonce=wTMKiRCNxXPV8ooXPQ0pYLtlbvlzfV0SsjeH4s9NpquArJjzWu9ZbUl6es4deVyb", " okta-oauth-state=X5AKl6fUj3hGDFf3BsGsnzxyXyMhuuc73lsvzYZHy5s8viUbZ8fUjNIZVJsMMxhs", " ln=anuj.sachan51@gmail.com"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment