Add one-time token login flow and deep-link handling
This commit is contained in:
47
App.js
47
App.js
@@ -34,6 +34,7 @@ import { Platform } from 'react-native';
|
||||
import { PostHogProvider } from 'posthog-react-native'
|
||||
import * as Updates from 'expo-updates';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
import * as Linking from 'expo-linking';
|
||||
|
||||
|
||||
const Tab = createBottomTabNavigator();
|
||||
@@ -60,6 +61,15 @@ Notifications.setNotificationHandler({
|
||||
},
|
||||
});
|
||||
|
||||
const parseTokenLoginUrl = (url = "") => {
|
||||
const parsed = Linking.parse(url);
|
||||
const token = typeof parsed?.queryParams?.token === "string" ? parsed.queryParams.token.trim() : "";
|
||||
if (!token) return null;
|
||||
const hostOrPath = `${parsed?.hostname || ""}/${parsed?.path || ""}`.toLowerCase();
|
||||
if (!hostOrPath.includes("token-login")) return null;
|
||||
return token;
|
||||
};
|
||||
|
||||
async function registerForPushNotificationsAsync() {
|
||||
|
||||
if (Platform.OS === 'android') {
|
||||
@@ -276,16 +286,51 @@ const MainNavigation = ({ route }) => {
|
||||
export default function App() {
|
||||
const appState = useSnapshot(GlobalState);
|
||||
const viewer = appState.me || {};
|
||||
const navigationRef = useRef(null);
|
||||
const pendingTokenRef = useRef(null);
|
||||
const hasUnviewedNotifications = Array.isArray(viewer?.notifications)
|
||||
? viewer.notifications.some((n) => n && n.viewed !== true)
|
||||
: false;
|
||||
|
||||
useEffect(() => {
|
||||
const routeTokenLogin = (url) => {
|
||||
const token = parseTokenLoginUrl(url);
|
||||
if (!token) return;
|
||||
if (!navigationRef.current) {
|
||||
pendingTokenRef.current = token;
|
||||
return;
|
||||
}
|
||||
navigationRef.current.navigate("Login", { token });
|
||||
};
|
||||
|
||||
Linking.getInitialURL().then((url) => {
|
||||
if (!url) return;
|
||||
routeTokenLogin(url);
|
||||
});
|
||||
|
||||
const sub = Linking.addEventListener("url", ({ url }) => {
|
||||
routeTokenLogin(url);
|
||||
});
|
||||
|
||||
return () => {
|
||||
sub.remove();
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<PaperProvider settings={{
|
||||
icon: props => <MaterialIcons {...props} />,
|
||||
}} theme={theme}>
|
||||
<KeyboardAvoidingView behavior={Platform.OS === 'ios' ? 'padding' : 'height'} style={{ flex: 1 }}>
|
||||
<NavigationContainer>
|
||||
<NavigationContainer
|
||||
ref={navigationRef}
|
||||
onReady={() => {
|
||||
if (!pendingTokenRef.current) return;
|
||||
const token = pendingTokenRef.current;
|
||||
pendingTokenRef.current = null;
|
||||
navigationRef.current?.navigate("Login", { token });
|
||||
}}
|
||||
>
|
||||
<PostHogProvider apiKey="phc_2zh7SoBDi83vaa7Rz4YWTXWCjV0bOLfiqRyUo2mkf0b" autocapture>
|
||||
<StatusBar style="dark" />
|
||||
<Stack.Navigator screenOptions={{
|
||||
|
||||
Reference in New Issue
Block a user