logout.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { useRouter, useNavigation } from 'app/lib/hooks/router'
  2. import { useEffect, useRef } from 'react';
  3. import { Text } from 'app/design/typography'
  4. import { View } from 'app/design/view'
  5. import { useTranslation } from 'react-i18next'
  6. import { useCurrentUser } from 'app/context/user'
  7. import { clearClientSessionState, resetNavigationAfterSignOut } from 'app/lib/platform/session-cleanup'
  8. export default function ElementLogout({ data }) {
  9. const { t } = useTranslation();
  10. const router = useRouter();
  11. const navigation = useNavigation();
  12. const { setCurrentUser } = useCurrentUser();
  13. const didCleanupRef = useRef(false);
  14. const uri = data?.uri === '/' || !data?.uri ? '/home' : data.uri;
  15. const timeout = data?.timeout;
  16. useEffect(() => {
  17. if (didCleanupRef.current) return;
  18. didCleanupRef.current = true;
  19. // Drop auth + all client caches before rewriting routes so the next
  20. // account cannot inherit page/query/tab state from the previous one.
  21. setCurrentUser(false);
  22. clearClientSessionState();
  23. const go = () => resetNavigationAfterSignOut(router, { url: uri, navigation });
  24. if (timeout) {
  25. const timeoutId = setTimeout(go, timeout);
  26. return () => clearTimeout(timeoutId);
  27. }
  28. go();
  29. }, [uri, timeout, router, navigation, setCurrentUser]);
  30. return (
  31. <View className="flex-1 items-center justify-center">
  32. <Text className="text-center text-foreground">{t('Logging out...')}</Text>
  33. </View>
  34. );
  35. }