Skip to content

Commit f41b954

Browse files
committed
feat: add user session management and logout on session expiration
1 parent dbdcac9 commit f41b954

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/providers/AuthProvider.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ import { firebaseAuth } from 'src/lib/firebase'
1313
export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
1414
const navigate = useNavigate()
1515
const [searchParams] = useSearchParams()
16-
const { closeAuthModal, initState, setAuthError, openAuthModal, setConnecting } = useAuth()
16+
const {
17+
closeAuthModal,
18+
initState,
19+
setAuthError,
20+
openAuthModal,
21+
setConnecting,
22+
logout,
23+
isConnected,
24+
} = useAuth()
1725

1826
const connectTheUser = useCallback((token?: string | null, provider?: string | null) => {
1927
const allowedProviders = ['google', 'github']
@@ -134,5 +142,23 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
134142
navigate(window.location.pathname, { replace: true })
135143
})
136144
}, [searchParams])
145+
146+
/**
147+
* This effect is used to check if the user is still connected on Firebase Auth
148+
* and logout the user if the session has expired
149+
*/
150+
useEffect(() => {
151+
const unsubscribe = firebaseAuth.onAuthStateChanged(async (user) => {
152+
if (!user) {
153+
if (isConnected) {
154+
toast('Session expired, please reconnect', { theme: 'dangerToast' })
155+
}
156+
157+
await logout()
158+
}
159+
})
160+
return () => unsubscribe()
161+
}, [isConnected])
162+
137163
return <>{children}</>
138164
}

0 commit comments

Comments
 (0)