react-native-cookies / cookies

🍪 Cookie Manager for React Native
MIT License
480 stars 95 forks source link

I am getting different tokken (xzcsrftoken) for the 1st time after user login in IOS app but it is working fine in android #176

Closed lakshyastech closed 1 year ago

lakshyastech commented 1 year ago

Here is my code

import {Alert, SafeAreaView, StyleSheet, View} from 'react-native'; import React, {useEffect, useRef} from 'react'; import WebView from 'react-native-webview'; import CookieManager from '@react-native-cookies/cookies'; import AsyncStorage from '@react-native-async-storage/async-storage'; import colors from '../../constant/colors'; import {useRecoilState} from 'recoil'; import {islogged} from '../../../providers/recoilStore'; import { Mixpanel } from "mixpanel-react-native"; const trackAutomaticEvents = true; const mixpanel = new Mixpanel( "bcdc122a574ce8f34047d4c0fc627849", trackAutomaticEvents ); mixpanel.init();

const Login = () => { const [isadt, setisadt] = React.useState(false); const [isbdt, setisbdt] = React.useState(false); const [xzcsrftoken, setxzcsrftoken] = React.useState(false); const [loggedIn, setLoggedIn] = useRecoilState(islogged);

const INJECTED_JAVASCRIPT = (function() { window.postMessage = function(data) { window.ReactNativeWebView.postMessage(data); }; })();;

const getcookie = () => { CookieManager.get('https://www.domain.in/signin') .then(async res => { for (var key in res) { if (key.startsWith('_iamadtclient')) { AsyncStorage.setItem('iamadt', JSON.stringify(res[key])); setisadt(true); } if (key.startsWith('_iambdtclient')) { AsyncStorage.setItem('iambdt', JSON.stringify(res[key])); setisbdt(true); } if (key.startsWith('csrfc')) { AsyncStorage.setItem('xzcsrftoken', JSON.stringify(res[key])); setxzcsrftoken(res[key]); } } if (isadt && isbdt && xzcsrftoken) { setLoggedIn(true); } }) .catch(err => { Alert.alert('Error', 'Something went wrong'); }); }; const webViewRef = useRef(null); const clearCookies = document.cookie.split(';').forEach(function(c) { document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/'); }); ; const clearWebViewCookies = () => { webViewRef.current.injectJavaScript(clearCookies); };

useEffect(() => { mixpanel.track("sign_in_screen"); getcookie(); }, []);

return (

{ getcookie(); }} javaScriptEnabled={true} domStorageEnabled={true} startInLoadingState={true} scalesPageToFit={true} mixedContentMode={'compatibility'} thirdPartyCookiesEnabled={true} sharedCookiesEnabled={true} cacheEnabled={true} cacheMode={'LOAD_DEFAULT'} allowFileAccess={true} allowUniversalAccessFromFileURLs={true} allowFileAccessFromFileURLs={true} allowContentAccess={true} injectedJavaScript={INJECTED_JAVASCRIPT} useWebKit={true} originWhitelist={['*']} ref={webViewRef} />

); }; export default Login;

TingzhaoW commented 1 year ago

+1 on this

caralin3 commented 7 months ago

@lakshyastech Did you find a solution to this?