Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Last active June 11, 2021 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heytulsiprasad/e11629dab6b174df5e724ebcec02671e to your computer and use it in GitHub Desktop.
Save heytulsiprasad/e11629dab6b174df5e724ebcec02671e to your computer and use it in GitHub Desktop.
Get customToken from URL and login using signInWithCustomToken
// Mobile: React Native
import React, {useState, useEffect} from 'react';
import { View } from 'react-native';
import {Button} from 'react-native-paper';
import { WebView } from 'react-native-webview';
const App = () => {
const [show, setShow] = useState({isOpen: false, url: null});
const [customToken, setCustomToken] = useState(null);
const handleWebViewNavigationStateChange = newNavState => {
const {url} = newNavState;
if (url.includes('customToken')) {
const token = url.split('customToken=')[1];
setCustomToken(token);
setShow({isOpen: false, url: null});
}
};
// LOGIN THE USER USING CUSTOM TOKEN
useEffect(() => {
// signInWithCustomToken
if (customToken) {
auth
.signInWithCustomToken(customToken)
.then(user => {
// eslint-disable-next-line no-alert
alert(`Logged in as ${auth.currentUser.email}`);
})
.catch(err => console.error(err));
}
}, [customToken]);
return (
<View>
<View>
<Button
onPress={() =>
setShow({
isOpen: true,
url: 'webview.com/login',
})
}>
Login
</Button>
</View>
{show.isOpen && (
<WebView
source={{uri: show.url}}
onNavigationStateChange={handleWebViewNavigationStateChange}
/>
)}
</ImageBackground>
</View>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment