Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
beasy-mobile
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
G
beasy-mobile
Commits
a526018f
Commit
a526018f
authored
Sep 09, 2024
by
G
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
navigation based on the user authentication state. Handle persistant reload auth state.
parent
470db41b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
107 additions
and
19 deletions
+107
-19
App.tsx
App.tsx
+6
-6
UserAuthenticationContext.tsx
src/contexts/UserAuthenticationContext.tsx
+68
-10
AppMainStackNavigator.tsx
src/navigations/AppMainStackNavigator.tsx
+33
-3
No files found.
App.tsx
View file @
a526018f
...
...
@@ -20,9 +20,9 @@ export default function App() {
<
ThemeProvider
theme=
{
theme
}
>
<
ModalsManagerProvider
>
<
SafeAreaProvider
>
<
NavigationContainer
>
<
ProvideQueryClient
>
<
UserAuthenticationContextProvid
er
>
<
ProvideQueryClient
>
<
UserAuthenticationContextProvider
>
<
NavigationContain
er
>
{
/* <View style={styles.container}> */
}
{
/* <SafeAreaProvider>
<StatusBar style="auto" />
...
...
@@ -30,9 +30,9 @@ export default function App() {
</SafeAreaProvider> */
}
<
AppMainStackNavigator
/>
{
/* </View> */
}
</
UserAuthenticationContextProvid
er
>
</
ProvideQueryClient
>
</
NavigationContainer
>
</
NavigationContain
er
>
</
UserAuthenticationContextProvider
>
</
ProvideQueryClient
>
</
SafeAreaProvider
>
</
ModalsManagerProvider
>
<
ModalContainer
/>
...
...
src/contexts/UserAuthenticationContext.tsx
View file @
a526018f
import
type
{
ImainStackNavigator
}
from
"@/navigations/Types"
;
import
type
{
IuserInformations
}
from
"@/utils/requests/Types"
;
import
authenticateUser
from
"@/utils/requests/authenticateUser"
;
import
getUserInformations
from
"@/utils/requests/userInformations"
;
import
{
LOG
}
from
"@logger"
;
import
{
type
NavigationProp
,
useNavigation
}
from
"@react-navigation/native"
;
import
AsyncStorage
from
"@react-native-async-storage/async-storage"
;
// import { type NavigationProp, useNavigation } from "@react-navigation/native";
import
{
useMutation
}
from
"@tanstack/react-query"
;
import
type
{
AxiosError
}
from
"axios"
;
import
{
createContext
,
useCallback
,
useContext
,
useState
}
from
"react"
;
import
*
as
SplashScreen
from
"expo-splash-screen"
;
import
{
createContext
,
useCallback
,
useContext
,
useEffect
,
useState
}
from
"react"
;
import
type
{
IauthenticationData
}
from
"./Types"
;
const
log
=
LOG
.
extend
(
"UserAuthenticationContext"
);
SplashScreen
.
preventAutoHideAsync
();
export
interface
UserAuthenticationContextProps
{
isAuthenticated
:
boolean
;
setIsAuthenticated
:
React
.
Dispatch
<
React
.
SetStateAction
<
boolean
>>
;
...
...
@@ -87,7 +90,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
// Hoooks
const
navigation
=
useNavigation
<
NavigationProp
<
ImainStackNavigator
>>
();
//
const navigation = useNavigation<NavigationProp<ImainStackNavigator>>();
// Mutations
...
...
@@ -133,7 +136,10 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
onSuccess
:
(
userInformations
)
=>
{
log
.
info
(
"getUserInformations request was a success, navigating to homepage"
);
setUserInformations
(
userInformations
);
navigation
.
navigate
(
"appBottomTabsNavigator"
);
setIsAuthenticated
(
true
);
storeAuthenticationData
(
authenticationData
);
storeUserInformations
(
userInformations
);
// navigation.navigate("appBottomTabsNavigator");
},
onError
:
(
error
)
=>
{
log
.
error
(
"userInformationsMutation"
,
error
);
...
...
@@ -179,11 +185,63 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
user
:
0
,
},
});
navigation
.
reset
({
index
:
0
,
routes
:
[{
name
:
"userLoginScreen"
}],
});
},
[
navigation
]);
// navigation.reset({
// index: 0,
// routes: [{ name: "userLoginScreen" }],
// });
},
[]);
// Storages
const
storeAuthenticationData
=
async
(
authenticationData
:
IauthenticationData
)
=>
{
try
{
await
AsyncStorage
.
setItem
(
"authenticationData"
,
JSON
.
stringify
(
authenticationData
));
}
catch
(
e
)
{
log
.
error
(
"storeAuthenticationData |"
,
e
);
// saving error
}
};
const
storeUserInformations
=
async
(
userInformations
:
IuserInformations
)
=>
{
try
{
await
AsyncStorage
.
setItem
(
"userInformations"
,
JSON
.
stringify
(
userInformations
));
}
catch
(
e
)
{
log
.
error
(
"storeUserInformations |"
,
e
);
// saving error
}
};
const
loadAuthenticationData
=
async
()
=>
{
log
.
debug
(
"loadAuthenticationData | Loading authentication data"
);
const
jsonRepresentation
=
await
AsyncStorage
.
getItem
(
"authenticationData"
);
return
jsonRepresentation
?
JSON
.
parse
(
jsonRepresentation
)
:
null
;
};
const
loadUserInformations
=
async
()
=>
{
log
.
debug
(
"loadUserInformations | Loading user informations"
);
const
jsonRepresentation
=
await
AsyncStorage
.
getItem
(
"userInformations"
);
return
jsonRepresentation
?
JSON
.
parse
(
jsonRepresentation
)
:
null
;
};
// biome-ignore lint/correctness/useExhaustiveDependencies: <This should only be executed once. At startup.>
useEffect
(()
=>
{
log
.
debug
(
"UserAuthenticationContext | App Startup | loading saved user data."
);
(
async
()
=>
{
try
{
const
authenticationData
=
await
loadAuthenticationData
();
const
userInformations
=
await
loadUserInformations
();
if
(
authenticationData
&&
userInformations
)
{
setAuthenticationData
(
authenticationData
);
setUserInformations
(
userInformations
);
setIsAuthenticated
(
true
);
// navigation.navigate("appBottomTabsNavigator");
}
}
catch
(
error
)
{
}
finally
{
await
SplashScreen
.
hideAsync
();
}
})();
},
[]);
return
(
<
UserAuthenticationContext
.
Provider
...
...
src/navigations/AppMainStackNavigator.tsx
View file @
a526018f
import
{
useUserAuthenticationContext
}
from
"@/contexts/UserAuthenticationContext"
;
import
{
LOG
}
from
"@logger"
;
import
{
createNativeStackNavigator
}
from
"@react-navigation/native-stack"
;
import
HomeUserNotLoggedIn
from
"@screens/HomeUserNotLoggedIn"
;
import
PaymentResultScreen
from
"@screens/PaymentResultScreen"
;
import
UserLoginScreen
from
"@screens/UserLoginScreen"
;
import
WaveQrCodePaymentScreen
from
"@screens/WaveQrCodePaymentScreen"
;
import
*
as
SplashScreen
from
"expo-splash-screen"
;
import
{
AppBottomTabsNavigator
}
from
"./AppBottomTabsNavigator"
;
import
type
{
ImainStackNavigator
}
from
"./Types"
;
const
Stack
=
createNativeStackNavigator
<
ImainStackNavigator
>
();
const
log
=
LOG
.
extend
(
"AppMainStackNavigator"
);
SplashScreen
.
preventAutoHideAsync
();
const
AppMainStackNavigator
=
()
=>
{
const
{
isAuthenticated
,
isAuthenticating
}
=
useUserAuthenticationContext
();
if
(
isAuthenticating
)
{
log
.
info
(
"isAuthenticating"
);
return
null
;
}
async
()
=>
{
await
SplashScreen
.
hideAsync
();
};
if
(
!
isAuthenticating
&&
!
isAuthenticated
)
{
log
.
info
(
"Navigating to UserLoginScreen"
);
return
(
<
Stack
.
Navigator
initialRouteName=
"homeUserNotLoggedIn"
screenOptions=
{
{
headerShown
:
false
}
}
>
<
Stack
.
Screen
name=
"homeUserNotLoggedIn"
component=
{
HomeUserNotLoggedIn
}
/>
<
Stack
.
Screen
name=
"userLoginScreen"
component=
{
UserLoginScreen
}
/>
</
Stack
.
Navigator
>
);
}
log
.
info
(
"Navigating to AppBottomTabsNavigator"
);
return
(
<
Stack
.
Navigator
initialRouteName=
"
homeUserNotLoggedIn
"
initialRouteName=
"
appBottomTabsNavigator
"
screenOptions=
{
{
headerShown
:
false
}
}
>
<
Stack
.
Screen
name=
"homeUserNotLoggedIn"
component=
{
HomeUserNotLoggedIn
}
/>
<
Stack
.
Screen
name=
"userLoginScreen"
component=
{
UserLoginScreen
}
/>
{
/*
<Stack.Screen name="homeUserNotLoggedIn" component={HomeUserNotLoggedIn} />
<Stack.Screen name="userLoginScreen" component={UserLoginScreen} />
*/
}
<
Stack
.
Screen
name=
"appBottomTabsNavigator"
component=
{
AppBottomTabsNavigator
}
/>
<
Stack
.
Screen
name=
"paymentResultScreen"
component=
{
PaymentResultScreen
}
/>
<
Stack
.
Screen
name=
"waveQrCodePaymentScreen"
component=
{
WaveQrCodePaymentScreen
}
/>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment