Last active
July 8, 2021 20:37
-
-
Save reberthkss/9bde3866b4c0cf854f98376a016861ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import React, {createContext, useCallback, useContext, useMemo, useState} from "react"; | |
import { Auth } from "aws-amplify"; | |
export enum AuthenticationStatus { | |
UNAUTHENTICATED, | |
SIGNING_IN, | |
WAITING_OTP, | |
CONFIRMING_OTP, | |
APPROVED_OTP, | |
AUTHENTICATED, | |
SIGNING_OUT, | |
SIGNING_UP | |
} | |
interface IAuthenticationPayload { | |
username: string, | |
password: string | |
} | |
type MethodSignIn = (username: string, password: string) => Promise<void>; | |
type MethodSignOut = () => Promise<void>; | |
type MethodSignUp = (username: string, password: string) => Promise<void>; | |
type MethodSetUsername = (username: string) => void; | |
type MethodSetPassword = (password: string) => void; | |
interface IAuthenticationState { | |
authenticationPayload: IAuthenticationPayload, | |
status: AuthenticationStatus, | |
signIn: MethodSignIn, | |
signOut: MethodSignOut, | |
signUp: MethodSignUp, | |
setUsername: MethodSetUsername, | |
setPassword: MethodSetPassword, | |
error: string | null | |
} | |
export const AuthenticationContext = createContext<IAuthenticationState | null>(null); | |
function useAuthenticationContext(): IAuthenticationState { | |
const [status, setStatus] = useState<AuthenticationStatus>(AuthenticationStatus.UNAUTHENTICATED); | |
const [error, setError] = useState<string | null>(null); | |
const [authenticationPayload, setAuthenticationPayload] = useState<IAuthenticationPayload>({username: "", password: ""}); | |
const signIn = useCallback(async (username: string, password: string) => { | |
// todo | |
}, [setStatus]); | |
const signUp = useCallback(async (username: string, password: string) => { | |
// todo | |
}, [setStatus]); | |
const signOut = useCallback(async() => { | |
// todo | |
}, []); | |
const setUsername = useCallback((username: string) => { | |
setAuthenticationPayload({ | |
...authenticationPayload, | |
username | |
}); | |
}, [setAuthenticationPayload, authenticationPayload]); | |
const setPassword = useCallback((password: string) => { | |
setAuthenticationPayload({ | |
...authenticationPayload, | |
password | |
}); | |
}, [setAuthenticationPayload, authenticationPayload]) | |
return useMemo(() => ( | |
{ | |
authenticationPayload, | |
status, | |
signIn, | |
signOut, | |
signUp, | |
setUsername, | |
setPassword, | |
error | |
} | |
), | |
[ | |
authenticationPayload, | |
status, | |
signIn, | |
signOut, | |
signUp, | |
setUsername, | |
setPassword, | |
error | |
]); | |
} | |
export const AuthenticationContextProvider: React.FC<any> = ({children}) => { | |
const authenticationContext = useAuthenticationContext(); | |
return ( | |
<AuthenticationContext.Provider value={authenticationContext}> | |
{children} | |
</AuthenticationContext.Provider> | |
) | |
} | |
export const useSafeAuthenticationContext = () => { | |
const authenticationContext = useContext(AuthenticationContext); | |
if (!authenticationContext) { | |
throw Error("This component must be wrap by AuthenticationContextProvider"); | |
} | |
return authenticationContext; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment