railbird-gql/App.tsx

49 lines
1.2 KiB
TypeScript
Raw Normal View History

import React, { useEffect } from "react";
2024-02-03 18:23:46 -07:00
import { ClientProvider, useAuthHeader } from "./graphql/client";
2024-02-03 20:30:00 -07:00
import AppNavigator from "./navigation/app-navigator";
2024-02-03 18:23:46 -07:00
import { DEV_USER_ID } from "@env";
2024-02-06 14:03:44 -07:00
import AsyncStorage from "@react-native-async-storage/async-storage";
2024-02-03 18:23:46 -07:00
// TODO: move to different file?
2024-02-03 18:23:46 -07:00
const SetAuthHeaderBasedOnEnv = () => {
const { setAuthHeader } = useAuthHeader();
useEffect(() => {
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 });
} else {
// Fetch token for authenticated users in production
2024-02-06 12:34:52 -07:00
const token = await AsyncStorage.getItem("token"); // get from not firebase auth ASYNC
if (token) {
console.log("Setting firebase auth token");
setAuthHeader({ key: "Authorization", value: `Bearer ${token}` });
}
}
};
setAuthAsync();
2024-02-03 18:23:46 -07:00
}, [setAuthHeader]);
return null;
};
2024-01-06 18:51:59 -07:00
const App: React.FC = () => {
2024-01-06 19:25:17 -07:00
return (
<ClientProvider>
2024-02-03 18:23:46 -07:00
<SetAuthHeaderBasedOnEnv />
<AppNavigator />
</ClientProvider>
2024-01-06 19:25:17 -07:00
);
2024-01-06 18:51:59 -07:00
};
2024-01-06 23:58:54 -07:00
export default function Root() {
return (
<React.StrictMode>
<App />
</React.StrictMode>
);
}