Skip to content

Commit cba5b35

Browse files
committed
add loading state outside AuthContext
1 parent 155766c commit cba5b35

File tree

7 files changed

+41
-42
lines changed

7 files changed

+41
-42
lines changed

src/App.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import { ModalsProvider } from '@mantine/modals';
66
import { AuthProvider } from './context/auth-context';
77
import { RouterProvider, createRouter } from '@tanstack/react-router';
88
import './index.css';
9+
import { LoadingOverlay } from '@mantine/core';
910

1011
import { routeTree } from './routeTree.gen';
11-
import { getAuth } from 'firebase/auth';
1212
import { theme } from './Theme';
13+
import { useAuthContext } from './hooks';
1314

1415
const queryClient = new QueryClient();
1516

1617
const router = createRouter({
1718
routeTree,
1819
context: {
1920
queryClient,
20-
auth: getAuth()
21+
auth: undefined
2122
},
2223
defaultPreload: 'intent',
2324
defaultPreloadStaleTime: 0
@@ -28,16 +29,24 @@ declare module '@tanstack/react-router' {
2829
router: typeof router;
2930
}
3031
}
32+
3133
const customTheme = createTheme(theme);
3234

35+
function AppWithRouter() {
36+
const auth = useAuthContext();
37+
if (auth.loading) {
38+
return <LoadingOverlay visible />;
39+
}
40+
return <RouterProvider router={router} context={{ auth }} />;
41+
}
42+
3343
export function App() {
3444
return (
3545
<QueryClientProvider client={queryClient}>
3646
<MantineProvider theme={customTheme}>
3747
<AuthProvider>
3848
<ModalsProvider>
39-
{/* <Loder /> */}
40-
<RouterProvider router={router} />
49+
<AppWithRouter />
4150
</ModalsProvider>
4251
</AuthProvider>
4352
</MantineProvider>

src/Theme/theme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const theme = {
1313
fontFamily: 'Nunito, sans-serif',
1414
headings: {
1515
fontFamily: 'Nunito, sans-serif',
16-
textWrap: 'wrap',
16+
textWrap: 'wrap' as "wrap" | "nowrap" | "balance" | "pretty" | "stable" | undefined,
1717
sizes: {
1818
h1: {
1919
fontSize: '2.5rem',

src/context/auth-context.tsx

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
1-
import { LoadingOverlay } from '@mantine/core';
21
import { auth } from '@notes/database';
2+
import { TContextAuth } from '@notes/types';
33
import {
44
signInWithEmailAndPassword,
55
User,
6-
UserCredential,
76
createUserWithEmailAndPassword,
87
updateProfile,
98
signOut
109
} from 'firebase/auth';
1110
import { createContext, useEffect, useState } from 'react';
1211

13-
type ContextAuth = {
14-
user: User | undefined;
15-
signIn: (email: string, password: string) => Promise<UserCredential>;
16-
signUp: (email: string, password: string, displayName: string) => Promise<UserCredential>;
17-
loading: boolean;
18-
setLoadingState: (loading: boolean) => void;
19-
signUserOut: () => Promise<void>;
20-
};
21-
22-
export const AuthContext = createContext<ContextAuth | undefined>(undefined);
12+
export const AuthContext = createContext<TContextAuth | undefined>(undefined);
2313

2414
export function AuthProvider({ children }) {
2515
const [user, setUser] = useState<User>();
@@ -59,7 +49,7 @@ export function AuthProvider({ children }) {
5949
return unsubscribe;
6050
}, []);
6151

62-
const value: ContextAuth = { user, signIn, signUp, signUserOut, loading, setLoadingState };
52+
const value: TContextAuth = { user, signIn, signUp, signUserOut, loading, setLoadingState };
6353

6454
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
6555
}

src/routes/__root.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
import { TContextAuth } from '@notes/types';
12
import { QueryClient } from '@tanstack/react-query';
23
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
3-
import { Outlet, Link, redirect, createRootRouteWithContext } from '@tanstack/react-router';
4+
import { Outlet, createRootRouteWithContext } from '@tanstack/react-router';
45
import { TanStackRouterDevtools } from '@tanstack/router-devtools';
5-
import { getAuth } from 'firebase/auth';
66

77
type RouterContext = {
8-
auth: ReturnType<typeof getAuth> | undefined;
8+
auth: TContextAuth | undefined;
99
queryClient: QueryClient;
1010
};
1111

1212
export const Route = createRootRouteWithContext<RouterContext>()({
13-
component: () => (
14-
<>
15-
<Outlet />
16-
<TanStackRouterDevtools />
17-
<ReactQueryDevtools initialIsOpen={false} />
18-
</>
19-
),
13+
component: () => {
14+
return (
15+
<>
16+
<Outlet />
17+
<TanStackRouterDevtools />
18+
<ReactQueryDevtools initialIsOpen={false} />
19+
</>
20+
);
21+
},
2022
notFoundComponent: () => {
2123
return <p>This is the notFoundComponent </p>;
2224
}

src/routes/_auth.index.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,5 @@ import { Home } from '@notes/views';
22
import { createFileRoute } from '@tanstack/react-router';
33

44
export const Route = createFileRoute('/_auth/')({
5-
// component: () => {
6-
// if (!isAuthenticated()) {
7-
// return <div style={{ color: 'white' }}>Loading...</div>;
8-
// }
9-
10-
// return <Home />;
11-
// },
12-
component: Home,
13-
pendingComponent: () => <div style={{ color: 'white' }}>Loading...</div>
5+
component: Home
146
});

src/routes/_auth.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createFileRoute, redirect } from '@tanstack/react-router';
33

44
export const Route = createFileRoute('/_auth')({
55
beforeLoad: async ({ context }) => {
6-
if (!context.auth?.currentUser) {
6+
if (!context.auth?.user) {
77
throw redirect({
88
to: RoutesDef.LOGIN
99
});

src/types/contexts.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export enum PageType {
2-
NOTES = 'notes',
3-
TODOS = "todos"
4-
}
1+
import { User, UserCredential } from 'firebase/auth';
2+
3+
export type TContextAuth = {
4+
user: User | undefined;
5+
signIn: (email: string, password: string) => Promise<UserCredential>;
6+
signUp: (email: string, password: string, displayName: string) => Promise<UserCredential>;
7+
loading: boolean;
8+
setLoadingState: (loading: boolean) => void;
9+
signUserOut: () => Promise<void>;
10+
};

0 commit comments

Comments
 (0)