Skip to content

Commit 2e98c6d

Browse files
authored
Revert "AYS-878 | show pop up about session expired for 401 errors (#359)"
This reverts commit 8bc13d3.
1 parent 238d727 commit 2e98c6d

File tree

5 files changed

+21
-100
lines changed

5 files changed

+21
-100
lines changed

src/components/ui/dialog.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ const DialogOverlay = React.forwardRef<
2929
))
3030
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName
3131

32-
type DialogContentProps = React.ComponentPropsWithoutRef<
33-
typeof DialogPrimitive.Content
34-
> & {
35-
showClose?: boolean
36-
}
37-
3832
const DialogContent = React.forwardRef<
3933
React.ElementRef<typeof DialogPrimitive.Content>,
40-
DialogContentProps
41-
>(({ className, children, showClose = true, ...props }, ref) => (
34+
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
35+
>(({ className, children, ...props }, ref) => (
4236
<DialogPortal>
4337
<DialogOverlay />
4438
<DialogPrimitive.Content
@@ -50,12 +44,10 @@ const DialogContent = React.forwardRef<
5044
{...props}
5145
>
5246
{children}
53-
{showClose && (
54-
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
55-
<X className="h-4 w-4" />
56-
<span className="sr-only">Close</span>
57-
</DialogPrimitive.Close>
58-
)}
47+
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
48+
<X className="h-4 w-4" />
49+
<span className="sr-only">Close</span>
50+
</DialogPrimitive.Close>
5951
</DialogPrimitive.Content>
6052
</DialogPortal>
6153
))

src/contexts/validateRouteProvider.tsx

Lines changed: 12 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
1-
'use client'
2-
1+
import React, { ReactNode, useState, useEffect, useMemo } from 'react'
2+
import { usePathname, useRouter } from 'next/navigation'
3+
import { useAppSelector } from '@/store/hooks'
4+
import { getRouteStatus } from '@/lib/getRouteStatus'
5+
import { getUserPermissions } from '@/lib/getUserPermissions'
6+
import { selectToken } from '@/modules/auth/authSlice'
37
import { LoadingSpinner } from '@/components/ui/loadingSpinner'
48
import {
59
ValidateRouteContext,
610
ValidateRouteContextType,
711
} from '@/contexts/validateRouteContext'
8-
import { getRouteStatus } from '@/lib/getRouteStatus'
9-
import { getUserPermissions } from '@/lib/getUserPermissions'
10-
import { selectToken } from '@/modules/auth/authSlice'
11-
import { useAppSelector } from '@/store/hooks'
12-
import { usePathname, useRouter } from 'next/navigation'
13-
import { ReactNode, useEffect, useMemo, useState } from 'react'
14-
15-
import {
16-
Dialog,
17-
DialogContent,
18-
DialogDescription,
19-
DialogHeader,
20-
DialogTitle,
21-
} from '@/components/ui/dialog'
22-
import { useTranslation } from 'react-i18next'
2312

2413
export const ValidateRouteProvider = ({
2514
children,
2615
}: {
2716
children: ReactNode
2817
}): React.JSX.Element => {
29-
const { t } = useTranslation()
3018
const router = useRouter()
3119
const pathname = usePathname()
3220
const token = useAppSelector(selectToken)
33-
3421
const [loading, setLoading] = useState(true)
35-
const [showModal, setShowModal] = useState(false)
36-
const [countdown, setCountdown] = useState(3)
37-
3822
const { isProtected, route, requiredPermission } = getRouteStatus(pathname)
3923
const userPermissions = token ? getUserPermissions(token) : []
4024

@@ -62,58 +46,17 @@ export const ValidateRouteProvider = ({
6246
setLoading(false)
6347
}
6448
} else if (isProtected) {
65-
setShowModal(true)
49+
router.replace('/login')
6650
} else {
6751
setLoading(false)
6852
}
69-
}, [token, pathname, isProtected, userHasPermission, router])
70-
71-
useEffect(() => {
72-
if (!showModal) return
53+
}, [token, pathname, isProtected, userHasPermission, router, loading])
7354

74-
if (showModal) {
75-
setCountdown(3)
76-
}
77-
78-
const interval = setInterval(() => {
79-
setCountdown((prev) => {
80-
if (prev === 1) {
81-
clearInterval(interval)
82-
setShowModal(false)
83-
router.replace('/login')
84-
}
85-
return prev - 1
86-
})
87-
}, 1000)
88-
89-
return () => clearInterval(interval)
90-
}, [showModal, router])
91-
92-
if (loading || showModal) {
55+
if (loading) {
9356
return (
94-
<>
95-
<Dialog open={showModal}>
96-
<DialogContent
97-
className="sm:max-w-md"
98-
showClose={false}
99-
onEscapeKeyDown={(e) => e.preventDefault()}
100-
onPointerDownOutside={(e) => e.preventDefault()}
101-
>
102-
<DialogHeader>
103-
<DialogTitle>{t('sessionExpired.title')}</DialogTitle>
104-
<DialogDescription>
105-
{t('sessionExpired.description')}
106-
<br />
107-
{t('sessionExpired.countdown', { count: countdown })}
108-
</DialogDescription>
109-
</DialogHeader>
110-
</DialogContent>
111-
</Dialog>
112-
113-
<div className="flex items-center justify-center h-screen">
114-
<LoadingSpinner />
115-
</div>
116-
</>
57+
<div className="flex items-center justify-center h-screen">
58+
<LoadingSpinner />
59+
</div>
11760
)
11861
}
11962

src/i18n/locales/en.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@
5959
"incorrectCredentials": "Incorrect email or password. Please try again."
6060
}
6161
},
62-
"sessionExpired": {
63-
"title": "Your Session Has Expired",
64-
"description": "Please log in again.",
65-
"countdown": "You will be redirected to the login screen in {{count}} second."
66-
},
6762
"password": {
6863
"forgot": {
6964
"title": "Forgot password",

src/i18n/locales/tr.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@
5959
"incorrectCredentials": "Hatalı e-posta veya şifre. Lütfen tekrar deneyiniz."
6060
}
6161
},
62-
"sessionExpired": {
63-
"title": "Oturum Süreniz Doldu",
64-
"description": "Lütfen tekrar giriş yapınız.",
65-
"countdown": "{{count}} saniye içinde giriş ekranına yönlendirileceksiniz…"
66-
},
6762
"password": {
6863
"forgot": {
6964
"title": "Şifrenizi mi unuttunuz?",

src/lib/showToast.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ export const showErrorToast = (
77
): void => {
88
const title = 'common.error.defaultTitle'
99

10-
const status = error?.response?.status
11-
12-
// error status 401 means session expired in this project 🤷‍♂️. Which we do not want to show toast.
13-
// Detailed info at https://afetyonetimsistemi.atlassian.net/browse/AYS-878
14-
if (status === 401) return
15-
16-
if (status === 429) description = 'common.error.tooManyRequest'
10+
if (error?.response?.status === 429) {
11+
description = 'common.error.tooManyRequest'
12+
}
1713

1814
toast({
1915
title,

0 commit comments

Comments
 (0)