From 957e1488f51ef4614c4b4610e343f85ba1ce74d9 Mon Sep 17 00:00:00 2001 From: Nigel Breslaw Date: Mon, 12 Feb 2024 22:33:14 +0100 Subject: [PATCH] Change how dispatch is done (#355) --- native_gg/App.tsx | 29 +++++++++++++++------ native_gg/src/authentication/AuthService.ts | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/native_gg/App.tsx b/native_gg/App.tsx index 72c678d80..149bc4c7f 100644 --- a/native_gg/App.tsx +++ b/native_gg/App.tsx @@ -2,7 +2,7 @@ import { StatusBar } from "expo-status-bar"; import { Button, StyleSheet, Text, View } from "react-native"; import { Image } from "expo-image"; import { LinearGradient } from "expo-linear-gradient"; -import { useEffect, useReducer } from "react"; +import { useEffect, useReducer, useRef } from "react"; import AuthUI from "./src/authentication/AuthUI.tsx"; import { clientID } from "./src/constants/env.ts"; import AuthService from "./src/authentication/AuthService.ts"; @@ -32,20 +32,22 @@ export default function App() { if (process.env.NODE_ENV === "development" && clientID === undefined) { console.warn("No .ENV file found. Please create one."); } - - const authService = AuthService.getInstance(); - + const authServiceRef = useRef(null); const [state, dispatch] = useReducer(reducer, initialState); useEffect(() => { + authServiceRef.current = AuthService.getInstance(); // Subscribe to auth changes - authService.subscribe(dispatch); + authServiceRef.current.subscribe(dispatch); // Unsubscribe when the component unmounts return () => { - authService.unsubscribe(); + if (authServiceRef.current) { + authServiceRef.current.unsubscribe(); + } + authServiceRef.current = null; }; - }, [authService.subscribe, authService.unsubscribe]); + }, []); return ( @@ -59,7 +61,18 @@ export default function App() { contentFit="contain" source="https://d33wubrfki0l68.cloudfront.net/554c3b0e09cf167f0281fda839a5433f2040b349/ecfc9/img/header_logo.svg" /> - authService.startAuth()} processURL={(url) => authService.processURL(url)} /> + { + if (authServiceRef.current) { + authServiceRef.current.startAuth(); + } + }} + processURL={(url) => { + if (authServiceRef.current) { + authServiceRef.current.processURL(url); + } + }} + /> Membership ID: {state.currentUserID} diff --git a/native_gg/src/authentication/AuthService.ts b/native_gg/src/authentication/AuthService.ts index 3a70b9841..60667f9f9 100644 --- a/native_gg/src/authentication/AuthService.ts +++ b/native_gg/src/authentication/AuthService.ts @@ -95,7 +95,7 @@ class AuthService { // Method to check if user data and auth token exist isAuthenticated(): boolean { const user = this.currentUserID; - console.log("isAuthenticated", user, this.authToken); + // console.log("isAuthenticated", user, this.authToken); return user && this.authToken ? true : false; }