auth work

This commit is contained in:
Romulus21
2024-07-17 07:09:04 +02:00
parent 6be48846b0
commit a34eadb51f
13 changed files with 1125 additions and 1042 deletions

View File

@@ -30,8 +30,9 @@ export const AuthUserProvider = ({children}: PropsWithChildren) => {
} catch (error) {
// @ts-expect-error 401 error to redirect
if (error.response.status === 401) {
console.info('no user login')
if (!['/connexion', '/sinscrire'].includes(window.location.pathname)) {
const page = window.location.pathname.split('/')[1]
if (!['connexion', 'sinscrire', 'reset'].includes(page)) {
console.info('no user login')
window.location.href = '/connexion'
}
}

View File

@@ -14,9 +14,13 @@ const useAxiosTools = (isLoading = false) => {
const errorCatch = (error: Error|AxiosError|unknown) => {
if (axios.isAxiosError(error)) {
(error.response?.status === 422)
? displayFormErrors(error)
: setError(error.response?.data.message || error.message)
if (error.response?.status === 422) {
displayFormErrors(error)
} else if (error.response?.status === 401) {
console.log('not authorise')
} else {
setError(error.response?.data.message || error.message)
}
} else if (error instanceof Error) {
setError(error.message)
}

View File

@@ -14,7 +14,7 @@ interface TrackerProps {
export const TrackerProvider = ({children}: PropsWithChildren) => {
const [currentTimeTracker, setCurrentTimeTracker] = useState<timeTracker|null>(null)
const {axiosGet, axiosPost, axiosDelete} = useAxiosTools()
const {axiosGet, axiosPost, axiosDelete, errorCatch} = useAxiosTools()
useEffect(() => {
fetchCurrentTimeTracker()
@@ -25,7 +25,7 @@ export const TrackerProvider = ({children}: PropsWithChildren) => {
const res = await axiosGet(`/api/time-trackers/user`)
setCurrentTimeTracker(res.data)
} catch (error) {
console.error(error)
errorCatch(error)
}
}