From 17b83212c62c61a2796c64bc34bd2127eb1c54da Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Wed, 17 Feb 2021 22:43:04 +0000 Subject: [PATCH] Preload auth context with user data from SSR --- app/src/frontend/app.tsx | 2 +- app/src/frontend/auth-context.tsx | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/src/frontend/app.tsx b/app/src/frontend/app.tsx index 62e09626..d23ce6d8 100644 --- a/app/src/frontend/app.tsx +++ b/app/src/frontend/app.tsx @@ -54,7 +54,7 @@ export const App: React.FC = props => { const mapAppPaths = ['/', '/:mode(view|edit|multi-edit)/:category/:building(\\d+)?/(history)?']; return ( - +
diff --git a/app/src/frontend/auth-context.tsx b/app/src/frontend/auth-context.tsx index c21729c0..f3f3edca 100644 --- a/app/src/frontend/auth-context.tsx +++ b/app/src/frontend/auth-context.tsx @@ -45,11 +45,16 @@ export const AuthContext = createContext({ const noop = () => {}; -export const AuthProvider: React.FC = ({ - children +interface AuthProviderProps { + preloadedUser?: User; +} + +export const AuthProvider: React.FC = ({ + children, + preloadedUser }) => { - const [isAuthenticated, setIsAuthenticated] = useState(false); - const [user, setUser] = useState(undefined); + const [isAuthenticated, setIsAuthenticated] = useState(preloadedUser != undefined); + const [user, setUser] = useState(preloadedUser); const [userError, setUserError] = useState(undefined); const [isLoading, setIsLoading] = useState(false); @@ -178,7 +183,10 @@ export const AuthProvider: React.FC = ({ // update user data initially to check if already logged in useEffect(() => { - updateUserData(); + // if user state not preloaded + if(user == undefined) { + updateUserData(); + } }, []); return (