-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add SessionExpirationTracker component * stores: update zustand import * Create wrapping AccountContext component. Makes it easier to use the accountStore (see https://docs.pmnd.rs/zustand/guides/initialize-state-with-props)
- Loading branch information
Showing
11 changed files
with
179 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { refresh } from "@jaws/auth/firestoreAuth"; | ||
import { ONE_HOUR_IN_MS, SESSION_LENGTH_IN_MS } from "@jaws/lib/helpers"; | ||
import { useAccountStore } from "@jaws/store/account/accountContext"; | ||
import { setCookies } from "cookies-next"; | ||
import { useState } from "react"; | ||
import { useClickAnyWhere, useInterval } from "usehooks-ts"; | ||
|
||
export interface Props { | ||
children?: any; | ||
} | ||
|
||
export const SessionExpirationTracker = ({ children }: Props) => { | ||
const [interval, setInterval] = useState(SESSION_LENGTH_IN_MS); | ||
const [user, setUser, logoutUser] = useAccountStore((state) => [ | ||
state.user, | ||
state.setUser, | ||
state.logoutUser, | ||
]); | ||
|
||
let latestInteraction = Date.now(); | ||
useClickAnyWhere(() => { | ||
latestInteraction = Date.now(); | ||
setInterval(0); | ||
|
||
async function doRefresh() { | ||
if ( | ||
!user || | ||
(user.sessionExpires && | ||
user.sessionExpires - Date.now() > SESSION_LENGTH_IN_MS) | ||
) { | ||
// Only refresh token if less then SESSION_LENGTH_IN_MS left | ||
return; | ||
} | ||
|
||
try { | ||
const newToken = await refresh(); | ||
if (newToken) { | ||
setCookies("idToken", newToken); | ||
setUser({ | ||
...user, | ||
sessionExpires: Date.now() + ONE_HOUR_IN_MS, | ||
}); | ||
} | ||
} catch (err) { | ||
console.log("Refresh token failed", JSON.stringify(err)); | ||
} | ||
} | ||
void doRefresh(); | ||
}); | ||
|
||
useInterval(() => { | ||
setInterval(SESSION_LENGTH_IN_MS); | ||
if (user && Date.now() - latestInteraction > SESSION_LENGTH_IN_MS) { | ||
// Session time expired - lets logout | ||
logoutUser(); | ||
return; | ||
} | ||
}, interval); | ||
return <>{children}</>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useAccountStore } from "@jaws/store/account/accountContext"; | ||
import { User } from "@jaws/store/account/accountStore"; | ||
import { AppProps } from "next/app"; | ||
import { ModalProvider } from "use-modal-hook"; | ||
import ErrorMessage from "../atoms/ErrorMessage"; | ||
import PageContainer from "../atoms/PageContainer"; | ||
import Navbar from "../organisms/Navbar"; | ||
|
||
export type AuthError = { | ||
message: string; | ||
code: string; | ||
}; | ||
export interface ExtendedAppProps extends AppProps { | ||
pageProps: { | ||
authedUser: User | null; | ||
authError?: AuthError; | ||
}; | ||
} | ||
|
||
export const AppWrapper = ({ Component, ...appProps }: ExtendedAppProps) => { | ||
const { authError } = appProps.pageProps; | ||
|
||
const isLoggedIn = useAccountStore((state) => state.isLoggedIn); | ||
|
||
return ( | ||
<> | ||
<Navbar /> | ||
{isLoggedIn ? ( | ||
<ModalProvider> | ||
<Component {...appProps.pageProps} /> | ||
</ModalProvider> | ||
) : ( | ||
<PageContainer> | ||
<h1>You need to Log in</h1> | ||
{authError && ( | ||
<ErrorMessage> | ||
{authError.message} {authError.code && `(${authError.code})`} | ||
</ErrorMessage> | ||
)} | ||
</PageContainer> | ||
)} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PropsWithChildren, useRef } from "react"; | ||
import { AccountContext } from "./accountContext"; | ||
import { AccountStore, createAccountStore, User } from "./accountStore"; | ||
|
||
type AccountProviderProps = PropsWithChildren<{ | ||
user: User | null; | ||
}>; | ||
|
||
export const AccountProvider = ({ | ||
children, | ||
...props | ||
}: AccountProviderProps) => { | ||
const storeRef = useRef<AccountStore>(); | ||
|
||
if (!storeRef.current) { | ||
storeRef.current = createAccountStore({ | ||
user: props.user, | ||
isLoggedIn: !!props.user, | ||
}); | ||
} | ||
|
||
return ( | ||
<AccountContext.Provider value={storeRef.current}> | ||
{children} | ||
</AccountContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { createContext, useContext } from "react"; | ||
import { useStore } from "zustand"; | ||
import { AccountState, AccountStore } from "./accountStore"; | ||
|
||
export const AccountContext = createContext<AccountStore | null>(null); | ||
|
||
export const useAccountStore = <T>( | ||
selector: (state: AccountState) => T, | ||
equalityFn?: (left: T, right: T) => boolean, | ||
) => { | ||
const store = useContext(AccountContext); | ||
if (!store) { | ||
throw new Error("Missing AccountContext.Provider in component tree"); | ||
} | ||
|
||
return useStore(store, selector, equalityFn); | ||
}; |
4 changes: 2 additions & 2 deletions
4
src/store/accountStore.ts → src/store/account/accountStore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters