mirror of
https://github.com/theoludwig/p61-project.git
synced 2024-07-17 07:00:12 +02:00
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
|
import { createContext, useContext, useEffect } from "react"
|
||
|
|
||
|
import { usePresenterState } from "@/presentation/react/hooks/usePresenterState"
|
||
|
import type {
|
||
|
AuthenticationPresenter,
|
||
|
AuthenticationPresenterState,
|
||
|
} from "@/presentation/presenters/Authentication"
|
||
|
import { authenticationPresenter } from "@/infrastructure"
|
||
|
|
||
|
export interface AuthenticationContextValue
|
||
|
extends AuthenticationPresenterState {
|
||
|
authenticationPresenter: AuthenticationPresenter
|
||
|
}
|
||
|
|
||
|
const defaultContextValue = {} as AuthenticationContextValue
|
||
|
const AuthenticationContext =
|
||
|
createContext<AuthenticationContextValue>(defaultContextValue)
|
||
|
|
||
|
interface AuthenticationProviderProps extends React.PropsWithChildren {}
|
||
|
|
||
|
export const AuthenticationProvider: React.FC<AuthenticationProviderProps> = (
|
||
|
props,
|
||
|
) => {
|
||
|
const { children } = props
|
||
|
|
||
|
useEffect(() => {
|
||
|
authenticationPresenter.initialAuthStateListener().catch((error) => {
|
||
|
console.error(error)
|
||
|
})
|
||
|
}, [])
|
||
|
|
||
|
const authenticationPresenterState = usePresenterState(
|
||
|
authenticationPresenter,
|
||
|
)
|
||
|
|
||
|
return (
|
||
|
<AuthenticationContext.Provider
|
||
|
value={{
|
||
|
...authenticationPresenterState,
|
||
|
authenticationPresenter,
|
||
|
}}
|
||
|
>
|
||
|
{children}
|
||
|
</AuthenticationContext.Provider>
|
||
|
)
|
||
|
}
|
||
|
|
||
|
export const useAuthentication = (): AuthenticationContextValue => {
|
||
|
const context = useContext(AuthenticationContext)
|
||
|
if (context === defaultContextValue) {
|
||
|
throw new Error(
|
||
|
"`useAuthentication` must be used within a `AuthenticationProvider`.",
|
||
|
)
|
||
|
}
|
||
|
return context
|
||
|
}
|