From 42bc37d2ffeac3c9ae6d139b4e1f2d2509f7f76b Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Thu, 12 Dec 2019 13:36:43 +0100 Subject: [PATCH] chore: update auth flow example --- example/src/Screens/AuthFlow.tsx | 104 +++++++++++++++++++------------ 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/example/src/Screens/AuthFlow.tsx b/example/src/Screens/AuthFlow.tsx index 14f9a21b..ba403dbe 100644 --- a/example/src/Screens/AuthFlow.tsx +++ b/example/src/Screens/AuthFlow.tsx @@ -9,11 +9,27 @@ import { } from '@react-navigation/stack'; type AuthStackParams = { - splash: undefined; - home: undefined; - 'sign-in': undefined; + Splash: undefined; + Home: undefined; + SignIn: undefined; + PostSignOut: undefined; }; +const AUTH_CONTEXT_ERROR = + 'Authentication context not found. Have your wrapped your components with AuthContext.Consumer?'; + +const AuthContext = React.createContext<{ + signIn: () => void; + signOut: () => void; +}>({ + signIn: () => { + throw new Error(AUTH_CONTEXT_ERROR); + }, + signOut: () => { + throw new Error(AUTH_CONTEXT_ERROR); + }, +}); + const SplashScreen = () => { return ( @@ -22,27 +38,27 @@ const SplashScreen = () => { ); }; -const SignInScreen = ({ onSignIn }: { onSignIn: (token: string) => void }) => { +const SignInScreen = () => { + const { signIn } = React.useContext(AuthContext); + return ( - ); }; -const HomeScreen = ({ onSignOut }: { onSignOut: () => void }) => { +const HomeScreen = () => { + const { signOut } = React.useContext(AuthContext); + return ( Signed in successfully 🎉 - @@ -105,36 +121,44 @@ export default function SimpleStackScreen({ navigation }: Props) { headerShown: false, }); + const authContext = React.useMemo( + () => ({ + signIn: () => dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' }), + signOut: () => dispatch({ type: 'SIGN_OUT' }), + }), + [] + ); + return ( - ( - navigation.goBack()} /> - ), - }} - > - {state.isLoading ? ( - - ) : state.userToken === undefined ? ( - - {() => ( - dispatch({ type: 'SIGN_IN', token })} - /> - )} - - ) : ( - - {() => ( - dispatch({ type: 'SIGN_OUT' })} /> - )} - - )} - + + ( + navigation.goBack()} /> + ), + }} + > + {state.isLoading ? ( + + ) : state.userToken === undefined ? ( + + ) : ( + + )} + + ); }