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
fbe2ec94
Commit
fbe2ec94
authored
Sep 09, 2024
by
G
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
logic for handling errors related to authentication
parent
eb01e000
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
19 deletions
+32
-19
UserAuthenticationContext.tsx
src/contexts/UserAuthenticationContext.tsx
+16
-19
authenticateUser.ts
src/utils/requests/authenticateUser.ts
+16
-0
No files found.
src/contexts/UserAuthenticationContext.tsx
View file @
fbe2ec94
import
authenticateUser
from
"@/utils/requests/authenticateUser"
;
import
authenticateUser
,
{
parseAuthicationErrors
}
from
"@/utils/requests/authenticateUser"
;
import
type
{
IuserInformations
}
from
"@/utils/requests/types"
;
import
type
{
IuserInformations
}
from
"@/utils/requests/types"
;
import
getUserInformations
from
"@/utils/requests/userInformations"
;
import
getUserInformations
from
"@/utils/requests/userInformations"
;
import
ErrorModal
from
"@components/modals/ErrorModal"
;
import
{
LOG
}
from
"@logger"
;
import
{
LOG
}
from
"@logger"
;
import
AsyncStorage
from
"@react-native-async-storage/async-storage"
;
import
AsyncStorage
from
"@react-native-async-storage/async-storage"
;
// import { type NavigationProp, useNavigation } from "@react-navigation/native";
// import { type NavigationProp, useNavigation } from "@react-navigation/native";
import
{
useMutation
}
from
"@tanstack/react-query"
;
import
{
useMutation
}
from
"@tanstack/react-query"
;
import
type
{
AxiosError
}
from
"axios
"
;
import
*
as
SplashScreen
from
"expo-splash-screen
"
;
import
{
createContext
,
useCallback
,
useContext
,
useEffect
,
useState
}
from
"react"
;
import
{
createContext
,
useCallback
,
useContext
,
useEffect
,
useState
}
from
"react"
;
import
{
useModalsManagerContext
}
from
"./ModalsManagerContext"
;
import
type
{
IauthenticationData
}
from
"./Types"
;
import
type
{
IauthenticationData
}
from
"./Types"
;
const
log
=
LOG
.
extend
(
"UserAuthenticationContext"
);
const
log
=
LOG
.
extend
(
"UserAuthenticationContext"
);
SplashScreen
.
preventAutoHideAsync
();
export
interface
UserAuthenticationContextProps
{
export
interface
UserAuthenticationContextProps
{
isAuthenticated
:
boolean
;
isAuthenticated
:
boolean
;
setIsAuthenticated
:
React
.
Dispatch
<
React
.
SetStateAction
<
boolean
>>
;
setIsAuthenticated
:
React
.
Dispatch
<
React
.
SetStateAction
<
boolean
>>
;
...
@@ -86,6 +90,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
...
@@ -86,6 +90,7 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
});
});
// Hoooks
// Hoooks
const
{
showModal
}
=
useModalsManagerContext
();
// const navigation = useNavigation<NavigationProp<ImainStackNavigator>>();
// const navigation = useNavigation<NavigationProp<ImainStackNavigator>>();
...
@@ -99,25 +104,16 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
...
@@ -99,25 +104,16 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
},
},
onSuccess
:
(
data
)
=>
{
onSuccess
:
(
data
)
=>
{
setAuthenticationData
(
data
);
setAuthenticationData
(
data
);
// navigation.popToTop();
// navigation.replace("bottomTabs");
// navigation.navigate("bottomTabs");
log
.
info
(
"Receive data from authenticateUser, running getUserInformations..."
);
log
.
info
(
"Receive data from authenticateUser, running getUserInformations..."
);
userInformationsMutation
.
mutate
(
data
.
access
);
userInformationsMutation
.
mutate
(
data
.
access
);
// console.log("user informations", userInformations);
},
},
// biome-ignore lint/suspicious/noExplicitAny: <Axios error>
onError
:
(
error
:
unknown
)
=>
{
onError
:
(
error
:
AxiosError
<
any
>
)
=>
{
const
errorString
=
parseAuthicationErrors
(
error
);
log
.
error
(
"authenticationMutation"
,
error
);
showModal
(<
ErrorModal
message=
{
errorString
}
/>);
if
(
error
.
response
)
{
},
log
.
error
(
"error :: "
,
error
.
response
.
data
);
onSettled
:
()
=>
{
if
(
error
.
response
.
status
===
400
)
{
setIsAuthenticating
(
false
);
return
setError
(
"Bad request"
);
}
const
message
:
string
=
error
.
response
.
data
.
detail
;
return
setError
(
message
);
}
setError
(
"Unknown error"
);
},
},
});
});
...
@@ -239,13 +235,14 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
...
@@ -239,13 +235,14 @@ export const UserAuthenticationContextProvider = ({ children }: { children: Reac
setAuthenticationData
(
authenticationData
);
setAuthenticationData
(
authenticationData
);
setUserInformations
(
userInformations
);
setUserInformations
(
userInformations
);
setIsAuthenticated
(
true
);
setIsAuthenticated
(
true
);
// navigation.navigate("appBottomTabsNavigator");
}
}
}
catch
(
error
)
{
}
catch
(
error
)
{
log
.
error
(
log
.
error
(
"UserAuthenticationContext | App Startup | error during retrieval of stored data |"
,
"UserAuthenticationContext | App Startup | error during retrieval of stored data |"
,
JSON
.
stringify
(
error
,
null
,
2
),
JSON
.
stringify
(
error
,
null
,
2
),
);
);
}
finally
{
setTimeout
(
async
()
=>
await
SplashScreen
.
hideAsync
(),
500
);
}
}
})();
})();
},
[]);
},
[]);
...
...
src/utils/requests/authenticateUser.ts
View file @
fbe2ec94
import
type
{
IauthenticationData
}
from
"@/contexts/Types"
;
import
type
{
IauthenticationData
}
from
"@/contexts/Types"
;
import
{
LOG
}
from
"@logger"
;
import
{
LOG
}
from
"@logger"
;
import
{
AxiosError
}
from
"axios"
;
import
axiosRequest
from
"../axiosRequest"
;
import
axiosRequest
from
"../axiosRequest"
;
const
log
=
LOG
.
extend
(
"authenticateUser"
);
const
log
=
LOG
.
extend
(
"authenticateUser"
);
...
@@ -19,3 +20,18 @@ const authenticateUser = async ({ username, password }: { username: string; pass
...
@@ -19,3 +20,18 @@ const authenticateUser = async ({ username, password }: { username: string; pass
};
};
export
default
authenticateUser
;
export
default
authenticateUser
;
export
const
parseAuthicationErrors
=
(
error
:
unknown
):
string
=>
{
if
(
error
instanceof
AxiosError
&&
error
.
response
)
{
switch
(
error
.
response
.
status
)
{
case
401
:
return
"Wrong username or password"
;
default
:
return
"Unknown Error. Please try again."
;
}
}
if
(
error
instanceof
AxiosError
&&
error
.
request
)
{
return
"Network error"
;
}
return
"Unknown error"
;
};
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