Skip to content

Commit

Permalink
feat: autoLogout
Browse files Browse the repository at this point in the history
  • Loading branch information
ddecrulle committed Jul 9, 2024
1 parent 039478d commit 091c63c
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@tanstack/react-router": "^1.43.14",
"axios": "^1.7.2",
"i18nifty": "^3.2.1",
"oidc-spa": "^4.8.0",
"oidc-spa": "^4.8.1",
"powerhooks": "^1.0.11",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
5 changes: 5 additions & 0 deletions src/i18n/resources/en.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const translations: Translations<'en'> = {
),
'operator logo alt': 'Insee, measure to understand',
},
AutoLogoutCountdown: {
'paragraph still there' : "Are you still there?"
,
"paragraph logged out in": ({secondsLeft}) => `You will be logged out in ${secondsLeft}`
},
ErrorComponent: {
'error button redirect to': ({ redirectTo }) => {
switch (redirectTo) {
Expand Down
5 changes: 5 additions & 0 deletions src/i18n/resources/fr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export const translations: Translations<'fr'> = {
),
'operator logo alt': 'Insee, mesurer pour comprendre',
},
AutoLogoutCountdown: {
'paragraph still there': 'Êtes-vous toujours là?',
'paragraph logged out in': ({ secondsLeft }) =>
`Vous serez déconnecté dans ${secondsLeft} secondes`,
},
ErrorComponent: {
'error button redirect to': ({ redirectTo }) => {
switch (redirectTo) {
Expand Down
5 changes: 5 additions & 0 deletions src/i18n/resources/sq.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const translations: Translations<'sq'> = {
),
'operator logo alt': 'Insee, mat për të kuptuar',
},
AutoLogoutCountdown: {
'paragraph still there': 'A jeni ende aty?',
'paragraph logged out in': ({ secondsLeft }) =>
`Do të dilni nga sistemi për ${secondsLeft} sekonda`,
},
ErrorComponent: {
'error button redirect to': ({ redirectTo }) => {
switch (redirectTo) {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Language = (typeof languages)[number]
export type ComponentKey =
| import('shared/components/Layout/Footer').I18n
| import('shared/components/Layout/Header').I18n
| import('shared/components/Layout/AutoLogoutCountdown').I18n
| import('shared/components/Error/ErrorComponent').I18n
| import('shared/components/Orchestrator/CustomPages/EndPage').I18n
| import('shared/components/Orchestrator/CustomPages/ValidationModal').I18n
Expand Down
2 changes: 2 additions & 0 deletions src/router/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { siteMapRoute } from 'pages/SiteMap/route'
import { visualizeRoute } from 'pages/Visualize/route'
import { Toaster } from 'react-hot-toast'
import { ErrorComponent } from 'shared/components/Error/ErrorComponent'
import { AutoLogoutCountdown } from 'shared/components/Layout/AutoLogoutCountdown'
import { Footer } from 'shared/components/Layout/Footer'
import { Header } from 'shared/components/Layout/Header'
import { NotFoundError } from 'shared/error/notFoundError'
Expand All @@ -35,6 +36,7 @@ function RootComponent() {
<Outlet />
</main>
<Footer />
<AutoLogoutCountdown />
</div>
)
}
Expand Down
75 changes: 75 additions & 0 deletions src/shared/components/Layout/AutoLogoutCountdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useTranslation } from 'i18n'
import { declareComponentKeys } from 'i18nifty'
import { useOidc } from 'oidc'
import { useEffect, useState } from 'react'

export function AutoLogoutCountdown() {
const { isUserLoggedIn, subscribeToAutoLogoutCountdown } = useOidc()
const [secondsLeft, setSecondsLeft] = useState<number | undefined>(undefined)

const { t } = useTranslation({ AutoLogoutCountdown })
useEffect(
() => {
if (!isUserLoggedIn) {
return
}

const { unsubscribeFromAutoLogoutCountdown } =
subscribeToAutoLogoutCountdown(({ secondsLeft }) =>
setSecondsLeft(
secondsLeft === undefined || secondsLeft > 60
? undefined
: secondsLeft
)
)

return () => {
unsubscribeFromAutoLogoutCountdown()
}
},
// NOTE: These dependency array could very well be empty
// we're just making react-hooks/exhaustive-deps happy.
// Unless you're hot swapping the oidc context isUserLoggedIn
// and subscribeToAutoLogoutCountdown never change for the
// lifetime of the app.
[isUserLoggedIn, subscribeToAutoLogoutCountdown]
)

if (secondsLeft === undefined) {
return null
}

return (
<div
// Full screen overlay, blurred background
style={{
position: 'fixed',
top: 0,
left: 0,
right: 0,
bottom: 0,
backgroundColor: 'rgba(0,0,0,0.5)',
backdropFilter: 'blur(10px)',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
zIndex: 1000,
}}
>
<div style={{ textAlign: 'center' }}>
<p>{t('paragraph still there')}</p>
<p>{t('paragraph logged out in', { secondsLeft })}</p>
</div>
</div>
)
}

const { i18n } = declareComponentKeys<
| 'paragraph still there'
| {
K: 'paragraph logged out in'
P: { secondsLeft: number }
R: string
}
>()({ AutoLogoutCountdown })
export type I18n = typeof i18n
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4264,10 +4264,10 @@ oidc-client-ts@2.4.0:
crypto-js "^4.2.0"
jwt-decode "^3.1.2"

oidc-spa@^4.8.0:
version "4.8.0"
resolved "https://registry.yarnpkg.com/oidc-spa/-/oidc-spa-4.8.0.tgz#9210a4a74cdf3ffbd77d8f57ae6ddec8538fb259"
integrity sha512-iX3/i0b0uh1yqLGQ/HGNlKcvai6oI+XEf69H5KK+xM6TbqSfXfxDxdYbE62HLv0LBr26xnK/S+tA2a7l7uC78w==
oidc-spa@^4.8.1:
version "4.8.1"
resolved "https://registry.yarnpkg.com/oidc-spa/-/oidc-spa-4.8.1.tgz#bef661b1a437c016db28818380ad0aee354931e5"
integrity sha512-RTZjakJ8lPOdSZoAsJ6mRP1+j/MukzNZ7ugh1qTrqQyQdhOlJPwHFT9OCevGYMRCsEEss/f74GYuKdT3bF3Org==
dependencies:
jwt-decode "^3.1.2"
oidc-client-ts "2.4.0"
Expand Down

0 comments on commit 091c63c

Please sign in to comment.