here you go loewy
This commit is contained in:
parent
b9221fa949
commit
3a476e0db4
@ -30,7 +30,7 @@ module.exports = {
|
||||
rules: {
|
||||
// Best Practices
|
||||
eqeqeq: ["error", "always"], // Enforce '===' instead of '=='
|
||||
curly: "error", // Require curly braces for all control statements
|
||||
curly: ["error", "multi-line", "consistent"], // Require curly braces for all control statements
|
||||
"no-unused-vars": "warn", // Warn about variables that are declared but not used
|
||||
|
||||
// React Specific
|
||||
|
@ -38,6 +38,8 @@ export const onAuthStateChanged = (
|
||||
return auth().onAuthStateChanged(callback);
|
||||
};
|
||||
|
||||
export const currentUser = () => auth().currentUser;
|
||||
|
||||
export const getCurrentUserToken = async (): Promise<string | null> => {
|
||||
const user = auth().currentUser;
|
||||
if (user) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import {
|
||||
confirmCode,
|
||||
getCurrentUserToken,
|
||||
currentUser,
|
||||
handleSignInWithPhoneNumber,
|
||||
handleSignOut,
|
||||
onAuthStateChanged,
|
||||
@ -8,7 +8,7 @@ import {
|
||||
|
||||
export {
|
||||
confirmCode,
|
||||
getCurrentUserToken,
|
||||
currentUser,
|
||||
handleSignInWithPhoneNumber,
|
||||
handleSignOut,
|
||||
onAuthStateChanged,
|
||||
|
@ -1,8 +1,7 @@
|
||||
import { DEV_USER_ID } from "@env";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { FirebaseAuthTypes } from "@react-native-firebase/auth";
|
||||
import React, { createContext, useContext, useEffect, useState } from "react";
|
||||
import { handleSignOut, onAuthStateChanged } from "../auth/firebase-auth";
|
||||
import { currentUser, handleSignOut, onAuthStateChanged } from "../auth";
|
||||
import { useAuthHeader } from "../graphql/client";
|
||||
|
||||
interface AuthContextType {
|
||||
@ -19,25 +18,44 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
}) => {
|
||||
const { setAuthHeader } = useAuthHeader();
|
||||
|
||||
const [user, setUser] = useState<FirebaseAuthTypes.User | null>(null);
|
||||
const [contextUser, setContextUser] = useState<FirebaseAuthTypes.User | null>(
|
||||
null,
|
||||
);
|
||||
const [isLoggedIn, setIsLoggedIn] = useState<boolean>(false);
|
||||
const [isLoading, setIsLoading] = useState<boolean>(true); // this is for a LoadingContext (auth, app reloads, foreground etc)
|
||||
|
||||
const authStateChangeCallback = async (user) => {
|
||||
console.log("user:", user);
|
||||
|
||||
const _completeAuthentication = (
|
||||
user: FirebaseAuthTypes.User,
|
||||
token: string,
|
||||
isLoggedIn: boolean,
|
||||
) => {
|
||||
setAuthHeader({ key: "Authorization", value: token });
|
||||
setContextUser(user);
|
||||
setIsLoggedIn(isLoggedIn);
|
||||
setIsLoading(false);
|
||||
};
|
||||
|
||||
const authStateChangeCallback = async (user: FirebaseAuthTypes.User) => {
|
||||
if (user) {
|
||||
const token = await user.getIdToken();
|
||||
if (token) {
|
||||
await AsyncStorage.setItem("token", token);
|
||||
setAuthHeader({ key: "Authorization", value: token });
|
||||
setUser(user);
|
||||
setIsLoggedIn(true);
|
||||
setIsLoading(false)
|
||||
}
|
||||
_completeAuthentication(user, token, true);
|
||||
} else {
|
||||
setIsLoggedIn(false);
|
||||
_completeAuthentication(undefined, undefined, false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
let unsubscribe = () => {
|
||||
console.log("Dev mode unsubscribe - really dense fn");
|
||||
};
|
||||
|
||||
if (!DEV_USER_ID) {
|
||||
unsubscribe = onAuthStateChanged(authStateChangeCallback);
|
||||
}
|
||||
|
||||
return unsubscribe;
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const setAuthAsync = async () => {
|
||||
@ -47,27 +65,19 @@ export const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
setAuthHeader({ key: "user_id", value: DEV_USER_ID });
|
||||
setIsLoggedIn(true);
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
setAuthAsync();
|
||||
}, [setAuthHeader]);
|
||||
|
||||
useEffect(() => {
|
||||
let unsubscribe: () => void = () => console.log('Dev mode unsubscribe - really dense fn')
|
||||
if (!DEV_USER_ID) {
|
||||
unsubscribe = onAuthStateChanged(authStateChangeCallback);
|
||||
}
|
||||
return unsubscribe;
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [])
|
||||
|
||||
const logOut = async () => {
|
||||
await AsyncStorage.removeItem("token");
|
||||
await handleSignOut();
|
||||
};
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isLoggedIn, isLoading, user, logOut }}>
|
||||
<AuthContext.Provider
|
||||
value={{ isLoggedIn, isLoading, user: contextUser, logOut }}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
@ -1,36 +1,39 @@
|
||||
import React, { createContext, useEffect, useState } from 'react';
|
||||
import { ActivityIndicator, StyleSheet, View } from 'react-native';
|
||||
import React, { createContext, useEffect, useState } from "react";
|
||||
import { ActivityIndicator, StyleSheet, View } from "react-native";
|
||||
|
||||
import { colors } from '../../styles';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { colors } from "../../styles";
|
||||
|
||||
const LoadingContext = createContext<boolean>(false);
|
||||
|
||||
export default function Loading({ children }) {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const checkHasToken = async () => {
|
||||
const hasToken = await AsyncStorage.getItem('token');
|
||||
// needs to connect to app state
|
||||
return hasToken
|
||||
}
|
||||
useEffect(() => {
|
||||
const checkHasToken = async () => {
|
||||
const hasToken = await AsyncStorage.getItem("token");
|
||||
// needs to connect to app state
|
||||
return hasToken;
|
||||
};
|
||||
|
||||
checkHasToken()
|
||||
.then(() => setLoading(false))
|
||||
}, []);
|
||||
checkHasToken().then(() => setLoading(false));
|
||||
}, []);
|
||||
|
||||
if (!loading) {
|
||||
return <LoadingContext.Provider value={loading}>{children}</LoadingContext.Provider>;
|
||||
}
|
||||
if (!loading) {
|
||||
return (
|
||||
<LoadingContext.Provider value={loading}>
|
||||
{children}
|
||||
</LoadingContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View style={s.container}>
|
||||
<ActivityIndicator size="large" color={colors.tournamentBlue} />
|
||||
</View>
|
||||
);
|
||||
return (
|
||||
<View style={s.container}>
|
||||
<ActivityIndicator size="large" color={colors.tournamentBlue} />
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
const s = StyleSheet.create({
|
||||
container: { flex: 1, justifyContent: 'center', alignItems: 'center' }
|
||||
});
|
||||
container: { flex: 1, justifyContent: "center", alignItems: "center" },
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user