From f849780aac95169b89f4ba6b42a1e8bcdbceb83f Mon Sep 17 00:00:00 2001 From: Loewy Date: Wed, 7 Feb 2024 22:06:19 -0800 Subject: [PATCH] some stuff --- src/context/auth-context.tsx | 38 ++++++++++++++++++++++-------------- src/screens/login.tsx | 1 - src/screens/profile.tsx | 10 +++------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/context/auth-context.tsx b/src/context/auth-context.tsx index d8c6b53..e86fa79 100644 --- a/src/context/auth-context.tsx +++ b/src/context/auth-context.tsx @@ -1,6 +1,12 @@ import { DEV_USER_ID } from "@env"; import { FirebaseAuthTypes } from "@react-native-firebase/auth"; -import React, { createContext, useContext, useEffect, useState } from "react"; +import React, { + createContext, + useCallback, + useContext, + useEffect, + useState, +} from "react"; import { handleSignOut, onAuthStateChanged } from "../auth"; import { useAuthHeader } from "../graphql/client"; @@ -24,16 +30,20 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ const [isLoggedIn, setIsLoggedIn] = useState(false); const [isLoading, setIsLoading] = useState(true); // this is for a LoadingContext (auth, app reloads, foreground etc) - const _completeAuthentication = ( - user: FirebaseAuthTypes.User, - token: string, - isLoggedIn: boolean, - ) => { - setAuthHeader({ key: "Authorization", value: token }); - setContextUser(user); - setIsLoggedIn(isLoggedIn); - setIsLoading(false); - }; + const _completeAuthentication = useCallback( + ( + user: FirebaseAuthTypes.User, + token: string, + isLoggedIn: boolean, + tokenType: "user_id" | "Authorization" = "Authorization", + ) => { + setAuthHeader({ key: tokenType, value: token }); + setContextUser(user); + setIsLoggedIn(isLoggedIn); + setIsLoading(false); + }, + [setAuthHeader], + ); const authStateChangeCallback = async (user: FirebaseAuthTypes.User) => { if (user) { @@ -61,13 +71,11 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({ const setAuthAsync = async () => { if (DEV_USER_ID) { console.log("Setting fake authorization user to: ", DEV_USER_ID); - setAuthHeader({ key: "user_id", value: DEV_USER_ID }); - setIsLoggedIn(true); - setIsLoading(false); + _completeAuthentication(null, DEV_USER_ID, true, "user_id"); } }; setAuthAsync(); - }, [setAuthHeader]); + }, [_completeAuthentication]); const logOut = async () => { await handleSignOut(); diff --git a/src/screens/login.tsx b/src/screens/login.tsx index b67246b..612aad7 100644 --- a/src/screens/login.tsx +++ b/src/screens/login.tsx @@ -1,4 +1,3 @@ -// Login.tsx import { FirebaseAuthTypes } from "@react-native-firebase/auth"; import React, { useState } from "react"; import { diff --git a/src/screens/profile.tsx b/src/screens/profile.tsx index 9e73b38..d0cbb65 100644 --- a/src/screens/profile.tsx +++ b/src/screens/profile.tsx @@ -13,12 +13,8 @@ export default function ProfileScreen() { {user && ( <> - - Display name: {user?.displayName} - - - Phone number: {user?.phoneNumber} - + Display name: {user?.displayName} + Phone number: {user?.phoneNumber} @@ -40,6 +36,6 @@ const styles = StyleSheet.create({ }, signOutButton: { paddingBottom: "5%", - paddingHorizontal: "25%" + paddingHorizontal: "25%", }, });