Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
106 additions
and
134 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
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,63 @@ | ||
import * as React from 'react'; | ||
import { getToken } from '../api/auth'; | ||
import { getObject, removeItem } from '../api/store'; | ||
import { AuthToken } from '../api/types'; | ||
|
||
type AuthContextType = { | ||
authToken?: AuthToken; | ||
loading: boolean; | ||
signIn: (u: string, p: string) => Promise<void>; | ||
signOut: () => void; | ||
}; | ||
|
||
export const AuthContext = React.createContext<AuthContextType>( | ||
{} as AuthContextType | ||
); | ||
|
||
export const AuthProvider = ({ children }: { children: JSX.Element }) => { | ||
const [authToken, setAuthToken] = React.useState<AuthToken>(); | ||
const [loading, setLoading] = React.useState<boolean>(true); | ||
|
||
React.useEffect(() => { | ||
const loadToken = async () => { | ||
try { | ||
const authData = (await getObject('auth_token')) as AuthToken; | ||
setAuthToken(authData); | ||
} catch (e) { | ||
// handle error | ||
} | ||
}; | ||
|
||
loadToken().then(() => setLoading(false)); | ||
}, []); | ||
|
||
const signIn = async (email: string, password: string) => { | ||
try { | ||
const authData = (await getToken(email, password)) as AuthToken; | ||
setAuthToken(authData); | ||
} catch (e) { | ||
// handle error | ||
} | ||
}; | ||
|
||
const signOut = async () => { | ||
setAuthToken(undefined); | ||
removeItem('auth_token'); | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{ authToken, loading, signIn, signOut }}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
}; | ||
|
||
export const useAuth = () => { | ||
const context = React.useContext(AuthContext); | ||
|
||
if (!context) { | ||
throw new Error('useAuth must be used within an AuthProvider'); | ||
} | ||
|
||
return context; | ||
}; |
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 |
---|---|---|
@@ -1,131 +1,36 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { | ||
createNativeStackNavigator, | ||
NativeStackScreenProps, | ||
} from '@react-navigation/native-stack'; | ||
import { createNativeStackNavigator } from '@react-navigation/native-stack'; | ||
import * as React from 'react'; | ||
import { RootLinkParamList } from '../types'; | ||
import BottomTabNavigator from './BottomTabNavigator'; | ||
import AuthNavigator from './AuthNavigator'; | ||
import { getObject } from '../api/store'; | ||
import { AuthResponse } from '../api/types'; | ||
import { AuthProvider, useAuth } from '../contexts/Auth'; | ||
|
||
const Root = createNativeStackNavigator<RootLinkParamList>(); | ||
const AuthContext = React.createContext<any>(() => {}); | ||
|
||
type AuthBaseState = { | ||
userToken?: string | null; | ||
isLoading?: boolean; | ||
isSignout?: boolean; | ||
}; | ||
|
||
type AuthState = AuthBaseState | undefined; | ||
|
||
type AuthActionTypes = 'RESTORE_TOKEN' | 'SIGN_IN' | 'SIGN_OUT'; | ||
|
||
type AuthData = { | ||
username: string; | ||
password: string; | ||
}; | ||
|
||
type AuthAction = { type: AuthActionTypes; token?: string }; | ||
|
||
type AuthReducer = (prevState: AuthState, action: AuthAction) => AuthState; | ||
|
||
export default function RootNavigator({ | ||
navigation, | ||
}: NativeStackScreenProps<RootLinkParamList>) { | ||
const [isAuth, setIsAuth] = React.useState<boolean>(); | ||
const [state, dispatch] = React.useReducer<AuthReducer>( | ||
(prevState, action) => { | ||
switch (action.type) { | ||
case 'RESTORE_TOKEN': | ||
return { | ||
...prevState, | ||
userToken: action.token, | ||
isLoading: false, | ||
}; | ||
case 'SIGN_IN': | ||
return { | ||
...prevState, | ||
userToken: action.token, | ||
isSignout: false, | ||
}; | ||
case 'SIGN_OUT': | ||
return { | ||
...prevState, | ||
userToken: null, | ||
isSignout: true, | ||
}; | ||
} | ||
}, | ||
{ | ||
isLoading: true, | ||
isSignout: false, | ||
userToken: null, | ||
} | ||
); | ||
|
||
React.useEffect(() => { | ||
const bootstrapAsync = async () => { | ||
let userToken: AuthResponse; | ||
|
||
try { | ||
userToken = (await getObject('auth_token')) as AuthResponse; | ||
} catch (e) { | ||
// Restoring token failed | ||
} | ||
|
||
// After restoring token, we may need to validate it in production apps | ||
|
||
// This will switch to the App screen or Auth screen and this loading | ||
// screen will be unmounted and thrown away. | ||
dispatch({ type: 'RESTORE_TOKEN', token: userToken.access_token }); | ||
}; | ||
|
||
bootstrapAsync(); | ||
}, []); | ||
|
||
const authContext = React.useMemo( | ||
() => ({ | ||
signIn: async (data: AuthData) => { | ||
// In a production app, we need to send some data (usually username, password) to server and get a token | ||
// We will also need to handle errors if sign in failed | ||
// After getting token, we need to persist the token using `SecureStore` | ||
// In the example, we'll use a dummy token | ||
|
||
dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' }); | ||
}, | ||
signOut: () => dispatch({ type: 'SIGN_OUT' }), | ||
signUp: async (data: AuthData) => { | ||
// In a production app, we need to send user data to server and get a token | ||
// We will also need to handle errors if sign up failed | ||
// After getting token, we need to persist the token using `SecureStore` | ||
// In the example, we'll use a dummy token | ||
|
||
dispatch({ type: 'SIGN_IN', token: 'dummy-auth-token' }); | ||
}, | ||
}), | ||
[] | ||
); | ||
export default function RootNavigator() { | ||
const { authToken, loading } = useAuth(); | ||
|
||
return ( | ||
<AuthContext.Provider value={authContext}> | ||
<Root.Navigator> | ||
{state?.userToken ? ( | ||
<Root.Screen | ||
name="BottomTab" | ||
component={BottomTabNavigator} | ||
options={{ headerShown: false }} | ||
/> | ||
) : ( | ||
<Root.Screen | ||
name="Auth" | ||
component={AuthNavigator} | ||
options={{ headerShown: false }} | ||
/> | ||
)} | ||
</Root.Navigator> | ||
</AuthContext.Provider> | ||
<AuthProvider> | ||
{loading ? ( | ||
<></> | ||
) : ( | ||
<Root.Navigator> | ||
{authToken?.access_token ? ( | ||
<Root.Screen | ||
name="BottomTab" | ||
component={BottomTabNavigator} | ||
options={{ headerShown: false }} | ||
/> | ||
) : ( | ||
<Root.Screen | ||
name="Auth" | ||
component={AuthNavigator} | ||
options={{ headerShown: false }} | ||
/> | ||
)} | ||
</Root.Navigator> | ||
)} | ||
</AuthProvider> | ||
); | ||
} |
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